SQL SELECT Statement: Projection, Aliasing & DISTINCT

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.


1. Basic Column Projection

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

2. Column Aliasing with the AS Keyword

Column 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;
Space in Aliases: If an alias contains spaces, wrap it in double quotes or single quotes: AS "Student Full Name".

3. Removing Duplicate Rows with SELECT DISTINCT

When 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

Literal Expressions & Server Health Checks

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!

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!