🌱BeginnerData Manipulation

SELECT Statement

Retrieve data from tables using SELECT, choosing specific columns and all rows.

Overview

SELECT is the most-used SQL statement. It retrieves rows and columns from one or more tables. At its simplest, SELECT * FROM table returns every column and every row. In practice, always name the specific columns you want SELECT * is convenient but fragile; schema changes can break applications and return unnecessary data. SELECT can also compute values inline you can do arithmetic, call functions, and combine string expressions without any table at all: SELECT 1 + 1 or SELECT NOW() work fine. This makes SELECT a general-purpose expression evaluator as well as a data retrieval tool. The full SELECT clause order is: SELECT β†’ FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ ORDER BY β†’ LIMIT. Understanding this clause order and the logical processing sequence helps write correct queries.

Key Points

  • β€’SELECT col1, col2 FROM table retrieves specific columns
  • β€’SELECT * FROM table retrieves all columns (avoid in production code)
  • β€’SELECT without FROM evaluates expressions: SELECT 1+1, NOW()
  • β€’Column aliases: SELECT price * 1.1 AS price_with_tax
  • β€’Clause order: SELECT β†’ FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ ORDER BY β†’ LIMIT

Examples

Basic SELECT patterns

-- All columns
SELECT * FROM products;

-- Specific columns only
SELECT id, name, price FROM products;

-- With expressions and alias
SELECT
  name,
  price,
  price * 1.2 AS price_with_vat
FROM products;

SELECT without a table

SELECT 42 AS answer;
SELECT NOW() AS current_time;
SELECT UPPER('hello') AS greeting;
SELECT 10 / 3 AS integer_division, 10.0 / 3 AS real_division;

Exercises

Write SQL to solve each exercise. Your progress is saved automatically.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

What is the correct order of clauses in a full SELECT statement?