The SELECT statement is the most frequently executed and versatile command in SQL. It forms the backbone of Data Query Language (DQL), allowing developers, analysts, and applications to retrieve stored data from one or more tables, perform arithmetic, compute calculated fields, and eliminate redundant rows.
To retrieve specific attributes from a table, list the column names separated by commas in the SELECT clause:
-- Query specific attributes from students
SELECT student_id, first_name, last_name, gpa
FROM students;
| student_id | first_name | last_name | gpa |
|---|---|---|---|
| 1 | Rahul | Sharma | 3.75 |
| 2 | Pooja | Patel | 3.92 |
| 3 | Aman | Verma | 3.85 |
AS KeywordColumn aliases assign temporary, descriptive labels to columns or computed expressions in the query output:
-- Provide clean, presentation-ready column headings
SELECT
student_id AS id,
CONCAT(first_name, ' ', last_name) AS full_name,
gpa * 25 AS percentage_estimate
FROM students;
AS "Student Full Name".
SELECT DISTINCTWhen querying categorical columns (like department or city), duplicate values are returned for every row. Use DISTINCT to return only unique values:
-- Return unique list of all courses offered
SELECT DISTINCT course
FROM students;
| course |
|---|
| BCA |
| B.Tech |
| MCA |
In SQL, you can use SELECT without a FROM clause to evaluate mathematical expressions or system functions (in Oracle, use the dummy table FROM dual):
-- Evaluate math expression and server time
SELECT
(150 * 12) AS annual_estimate,
CURRENT_DATE AS today,
VERSION() AS db_version;
Now let us learn how to filter data rows using the SQL WHERE Clause!