SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The SELECT
statement is a foundational component of SQL (Structured Query Language) and is used to fetch data from a database. When you execute a SELECT
statement, the result is presented as a result set, which is essentially a table of data returned by the query.
SELECT column1, column2, ... FROM table_name WHERE condition;
SELECT Clause: Lists the columns that you want to retrieve. You can also use the *
wildcard to select all columns from the table.
FROM Clause: Specifies the table from which you want to retrieve data.
WHERE Clause: Optional. Filters the result set to include only rows where the specified condition is true.
Select All Columns from a Table:
SELECT * FROM employees;
Select Specific Columns:
SELECT first_name, last_name FROM employees;
Using the WHERE Clause:
SELECT first_name, last_name FROM employees WHERE department_id = 10;
Ordering Results: You can use the ORDER BY
clause to order the result set:
SELECT first_name, last_name FROM employees ORDER BY last_name;
Limiting Results: Depending on the RDBMS, you might use LIMIT
, FETCH
, or other clauses to limit the number of rows returned:
MySQL:
SELECT first_name, last_name FROM employees LIMIT 5;
SQL Server:
SELECT TOP 5 first_name, last_name FROM employees;
Oracle (using FETCH FIRST):
SELECT first_name, last_name FROM employees FETCH FIRST 5 ROWS ONLY;
Filtering with Multiple Conditions: You can use logical operators like AND
and OR
:
SELECT first_name, last_name FROM employees WHERE department_id = 10 AND salary > 50000;
Aggregate Functions: Use functions like SUM
, AVG
, COUNT
, etc., to perform calculations:
SELECT AVG(salary) AS average_salary FROM employees;
Remember to always be specific about the columns you're selecting, especially when working with large databases. Using SELECT *
can be resource-intensive and slow down query performance.
The way data is retrieved and presented can be further refined using joins, groupings, having clauses, etc., making SQL a very flexible tool for data manipulation and analysis.
Properly indexing your database tables can greatly improve the performance of your SELECT
queries.
The SELECT
statement is just the tip of the iceberg when it comes to SQL's capabilities. Combining it with other clauses and SQL constructs can create very complex and powerful queries.
How to use SELECT in SQL:
SELECT column1, column2 FROM example_table;
Retrieving all columns with SELECT *:
SELECT * FROM example_table;
Specifying columns in the SELECT statement:
SELECT column1, column2 FROM example_table;
Using DISTINCT with SELECT in SQL:
SELECT DISTINCT column1 FROM example_table;
WHERE clause in SQL SELECT queries:
SELECT column1, column2 FROM example_table WHERE column3 = 'value';
ORDER BY clause in SQL SELECT:
SELECT column1, column2 FROM example_table ORDER BY column1 DESC;
LIMIT and OFFSET with SELECT in SQL:
SELECT column1, column2 FROM example_table LIMIT 10 OFFSET 5;
Filtering and sorting data with SELECT:
SELECT column1, column2 FROM example_table WHERE column3 = 'value' ORDER BY column1;
JOIN operations with SELECT in SQL:
SELECT t1.column1, t2.column2 FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id;
GROUP BY and aggregate functions in SELECT:
SELECT column1, COUNT(column2) AS count_column2 FROM example_table GROUP BY column1;
HAVING clause in SELECT queries:
SELECT column1, COUNT(column2) AS count_column2 FROM example_table GROUP BY column1 HAVING COUNT(column2) > 5;
Subqueries in SELECT statements:
SELECT column1, (SELECT MAX(column2) FROM another_table) AS max_column2 FROM example_table;
Using UNION and UNION ALL in SELECT:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
Advanced features of the SQL SELECT statement:
SELECT column1, CASE WHEN column2 > 10 THEN 'High' ELSE 'Low' END AS category, ROW_NUMBER() OVER (ORDER BY column1) AS row_num FROM example_table;