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!
SELECT * retrieves every column from the table, including unneeded large TEXT/BLOB fields. This increases network bandwidth consumption, exhausts database buffer cache memory, and breaks covering index optimizations. Always name the specific columns you need.
SELECT DISTINCT inspects all projected columns in the result set and filters out any rows where all selected column values are identical.
Yes! SQL supports standard arithmetic operators (+, -, *, /) directly on numeric columns (e.g., SELECT salary, salary * 1.10 AS revised_salary FROM employees;).
Proceed to the next class in sequence to build production mastery of relational database architecture.
Continue to Class 17: WHERE Clause → Previous: ← INSERT Statement | Full Syllabus
Filtering selected rows using conditional predicates.
Sorting output ascending or descending across multiple columns.
Paginating large query result sets for web interfaces.
Projecting columns from multiple related tables in one query.