Learn SQL  /  Getting Started  /  SELECT Statement

SELECT Statement

SELECT is the single most-used SQL statement โ€” it retrieves data from one or more tables, and every other clause you learn attaches to it.

Selecting Specific Columns

R Code
SELECT name, score
FROM students;
Output
name  | score
Rafi  | 88
Meera | 92
Zayn  | 76
Nadia | 65
💡 Tip
Avoid SELECT * in real applications - naming exactly the columns you need is faster for the database, and makes it obvious to anyone reading the query what data is actually being used.

Column Aliases with AS

R Code
SELECT name AS student_name, score AS exam_score
FROM students;
Output
student_name | exam_score
Rafi         | 88
Meera        | 92
Zayn         | 76
Nadia        | 65

Calculated Columns

A SELECT column does not have to already exist in the table - you can compute new values on the fly.

R Code
SELECT name, score, score / 20.0 AS grade_point
FROM students;
Output
name  | score | grade_point
Rafi  | 88    | 4.4
Meera | 92    | 4.6
Zayn  | 76    | 3.8
Nadia | 65    | 3.25

DISTINCT โ€” Removing Duplicate Rows

R Code
SELECT DISTINCT class
FROM students;
Output
class
A
B

Advanced: Concatenating Text

🚀 Advanced Example
String concatenation syntax differs slightly by database - most use ||, while MySQL uses the CONCAT() function.
R Code
-- standard SQL / PostgreSQL / SQLite
SELECT name || " (" || class || ")" AS label
FROM students;

-- MySQL equivalent
SELECT CONCAT(name, " (", class, ")") AS label
FROM students;
Output
label
Rafi (A)
Meera (A)
Zayn (B)
Nadia (B)

Key Points

  • List specific columns instead of SELECT * for clarity and performance.
  • AS renames a column or expression in the result (the alias) without changing the underlying table.
  • SELECT columns can be computed expressions, not just raw table columns.
  • DISTINCT removes duplicate rows from the result.