SQL SELECT Statement: Syntax, Usage, and Examples
The SQL SELECT statement lets you retrieve data from one or more tables. It’s the foundation of almost every SQL query you’ll write. Whether you’re pulling a few rows or joining multiple tables, this command puts the power of data at your fingertips.
How to Use the SQL SELECT Statement
Here’s the basic syntax of a SQL select statement:
sql
SELECT column1, column2, ...
FROM table_name;
You can use *
to select all columns from a table:
sql
SELECT * FROM employees;
To filter results, add a WHERE
clause:
sql
SELECT name, salary
FROM employees
WHERE department = 'Engineering';
You’re not limited to just one table or simple conditions. As your queries grow more advanced, so will your use of the SQL SELECT STATEMENT.
When to Use the SQL SELECT Statement
Retrieve Specific Columns
Sometimes, you only need a few columns from a table—not everything.
sql
SELECT first_name, last_name
FROM users;
This reduces clutter and improves query performance.
Filter Rows with WHERE
The SQL SELECT WHERE statement lets you grab only the rows you care about.
sql
SELECT * FROM orders
WHERE status = 'shipped';
This is essential for working with large datasets.
Combine Columns or Values
Use expressions in your select clause:
sql
SELECT first_name || ' ' || last_name AS full_name
FROM customers;
Or calculate values:
sql
SELECT product, price * quantity AS total
FROM sales;
The flexibility of the SQL SELECT statement really shines here.
Use SQL IF Statement in SELECT
You can include conditional logic in your query using CASE
:
sql
SELECT name,
CASE
WHEN score >= 60 THEN 'Pass'
ELSE 'Fail'
END AS result
FROM tests;
This lets you label or transform data on the fly.
Examples of the SQL SELECT Statement
Basic SELECT
sql
SELECT id, name FROM students;
You’re pulling specific fields for a quick view.
SELECT with Aliases
sql
SELECT name AS employee_name, salary AS monthly_salary
FROM employees;
Aliases clean up your output and make it easier to read.
SELECT with Calculated Fields
sql
SELECT item, price, price * 0.15 AS tax
FROM products;
Here you add a calculated column for tax without modifying the original table.
SELECT with SQL Nested Select Statements
You can use subqueries to pull dynamic results:
sql
SELECT name
FROM employees
WHERE department_id = (
SELECT id FROM departments WHERE name = 'Sales'
);
This setup pulls all employees who work in Sales, even if the department ID changes.
SELECT with Parameters (for Prepared Statements)
In applications, you often pass parameters into select statements:
sql
SELECT * FROM users WHERE id = ?;
This placeholder is filled at runtime. It keeps your queries secure and reusable.
Learn More About the SQL SELECT Statement
Sorting Results with ORDER BY
Use ORDER BY
to sort results by one or more columns:
sql
SELECT name, score
FROM students
ORDER BY score DESC;
Sorting is key for reports and user-friendly results.
Limit Output with LIMIT or TOP
If you only need a few rows:
sql
-- MySQL, PostgreSQL
SELECT * FROM users
LIMIT 10;
-- SQL Server
SELECT TOP 10 * FROM users;
This helps avoid overwhelming the application or UI.
Group Data with GROUP BY
When using aggregates like SUM()
or AVG()
, add GROUP BY
:
sql
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;
You’ll often combine this with HAVING to filter the groups.
Filter Groups with HAVING
Use HAVING
for group-level conditions:
sql
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
Without HAVING
, you can’t apply conditions to aggregates.
Join Tables in SELECT
The select statement SQL syntax also works with joins. Combine data from multiple tables like this:
sql
SELECT employees.name, departments.name AS dept
FROM employees
JOIN departments ON employees.department_id = departments.id;
Joins let you build powerful queries across related tables.
Change Data Type in SELECT
You can use functions like CAST()
or CONVERT()
to change a column’s type:
sql
SELECT CAST(salary AS VARCHAR) AS salary_text
FROM employees;
This is useful when formatting data or preparing it for display.
Use DISTINCT for Unique Values
When you want only unique entries, use DISTINCT
:
sql
SELECT DISTINCT country FROM customers;
It removes duplicates from the results.
Combine SELECT Statements with UNION
Want to stack results from two queries? Use UNION
:
sql
SELECT name FROM customers
UNION
SELECT name FROM vendors;
This brings both groups together, skipping duplicates. Use UNION ALL
to keep them.
Use SELECT in Stored Procedures and Views
You’ll often use select statements inside stored procedures, views, and functions:
sql
CREATE VIEW active_users AS
SELECT * FROM users WHERE active = 1;
This lets you reuse common queries across your database.
The SQL select statement forms the core of how you access and display data. From filtering and sorting to joining and nesting, this statement unlocks a huge part of what SQL can do.