Skip to content
  • SELECT Basics · SQL

SELECT Basics

LearnViaNotes @learnvianotes· 2 min read

SELECT reads data: pick columns, compute expressions, and shape the result set. Every other query feature builds on this.

Anatomy of a SELECT

SQL
SELECT   column_list        -- what to return   (required)
FROM     table_name         -- where from
WHERE    row_filter         -- which rows
GROUP BY grouping_columns   -- collapse rows into groups
HAVING   group_filter       -- which groups
ORDER BY sort_keys          -- in what order
LIMIT    n;                 -- how many

Only SELECT is mandatory — but the clauses must appear in exactly this order.

Choosing columns

SQL
SELECT name, salary, hire_date
FROM employees
ORDER BY emp_id
LIMIT 4;
Output
name  | salary | hire_date
------+--------+-----------
Asha  | 150000 | 2015-03-01
Ravi  | 120000 | 2017-06-15
Meera | 95000  | 2019-01-10
Karan | 95000  | 2020-08-01

Computed columns and literals

The select list can contain any expression: arithmetic, function calls, string concatenation (||), constants.

SQL
SELECT name,
       salary,
       salary / 12            AS monthly,
       salary * 12 / 100      AS pf_12_pct,
       'INR'                  AS currency,
       upper(name) || ' #' || emp_id AS badge
FROM employees
WHERE dept_id = 1
ORDER BY emp_id;
Output
name  | salary | monthly | pf_12_pct | currency | badge
------+--------+---------+-----------+----------+---------
Asha  | 150000 | 12500   | 18000     | INR      | ASHA #1
Ravi  | 120000 | 10000   | 14400     | INR      | RAVI #2
Meera | 95000  | 7916    | 11400     | INR      | MEERA #3
Karan | 95000  | 7916    | 11400     | INR      | KARAN #4

SELECT without FROM

SQL
SELECT 2 + 3 * 4 AS result, 7 / 2 AS int_div, 7 / 2.0 AS real_div, 'hi' || '!' AS greeting;
Output
result | int_div | real_div | greeting
-------+---------+----------+---------
14     | 3       | 3.5      | hi!

SELECT *

Is SQL case-sensitive?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report