Member-only story
The SELECT
statement in SQL is used to query a database and retrieve data from one or more tables. Here's a comprehensive guide covering various aspects of the SELECT
statement and its usage in different situations:
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC | DESC];
1. Selecting All Columns from a Table:
SELECT *
FROM table_name;
2. Selecting Specific Columns:
SELECT column1, column2
FROM table_name;
3. Renaming Columns (Alias):
SELECT column1 AS alias1, column2 AS alias2
FROM table_name;
4. Filtering Rows with WHERE Clause:
SELECT column1, column2
FROM table_name
WHERE condition;
5. Sorting Results with ORDER BY:
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC; -- or DESC for descending order