- Aggregate functions
- AVERAGE function
- BETWEEN operator
- CASE expression
- CAST() function
- COALESCE() function
- Comment
- Common table expression
- Constraints
- CONVERT function
- Cursor
- Data types
- Date functions
- DELETE statement
- DROP TABLE statement
- EXISTS operator
- HAVING clause
- IF statement
- Index
- IS NOT NULL condition
- IS NULL condition
- Joins
- LAG function
- LENGTH() function
- LIKE operator
- MERGE statement
- Normalization
- Not equal
- Operators
- ORDER BY clause
- Partition
- Pivot table
- Regex
- REPLACE function
- ROUND function
- SELECT DISTINCT clause
- SELECT statement
- Set operators
- Stored procedure
- String functions
- Subquery
- Substring
- Temporary table
- Transaction
- Trigger
- TRUNCATE TABLE
- UPDATE statement
- Views
- WHERE clause
- Window functions
SQL
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:
SELECT column1, column2, ...
FROM table_name;
You can use *
to select all columns from a table:
SELECT * FROM employees;
To filter results, add a WHERE
clause:
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.
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.
SELECT * FROM orders
WHERE status = 'shipped';
This is essential for working with large datasets.
Combine Columns or Values
Use expressions in your select clause:
SELECT first_name || ' ' || last_name AS full_name
FROM customers;
Or calculate values:
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
:
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
SELECT id, name FROM students;
You’re pulling specific fields for a quick view.
SELECT with Aliases
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
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:
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:
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:
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:
-- 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
:
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:
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:
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:
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
:
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
:
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:
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.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.