SQL

Basic Queries

SQL · 10 entries

SELECT All Columns

syntax
SELECT * FROM table_name;
example
SELECT * FROM users;
output
-- Returns all columns and rows from the users table

Note Avoid SELECT * in production code. Always specify columns to reduce data transfer and prevent breakage when table schema changes.

SELECT Specific Columns

syntax
SELECT column1, column2 FROM table_name;
example
SELECT first_name, email FROM users;
output
-- first_name | email
-- Alice      | [email protected]
-- Bob        | [email protected]

Note Listing columns explicitly improves readability and query performance since the database only fetches what you need.

WHERE Clause

syntax
SELECT columns FROM table WHERE condition;
example
SELECT first_name, email
FROM users
WHERE is_active = true;
output
-- Only rows where is_active is true

Note WHERE filters rows before any grouping occurs. Use HAVING to filter after GROUP BY.

ORDER BY

syntax
SELECT columns FROM table ORDER BY column [ASC|DESC];
example
SELECT product_name, price
FROM products
ORDER BY price DESC;
output
-- Products sorted from most expensive to cheapest

Note ASC is the default direction. You can sort by multiple columns: ORDER BY category ASC, price DESC. Sorting happens after WHERE filtering.

LIMIT and OFFSET

syntax
SELECT columns FROM table LIMIT count OFFSET skip;
example
SELECT product_name, price
FROM products
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;
output
-- Returns rows 21-30 (skips first 20, then takes 10)

Note MySQL/PostgreSQL use LIMIT. SQL Server uses TOP or FETCH FIRST. High OFFSET values degrade performance because the database still scans skipped rows.

DISTINCT

syntax
SELECT DISTINCT column FROM table;
example
SELECT DISTINCT city
FROM users
ORDER BY city;
output
-- Returns each city only once, no duplicates

Note DISTINCT applies to the entire row when used with multiple columns. SELECT DISTINCT city, state treats (city, state) pairs as the unit of uniqueness.

Column and Table Aliases

syntax
SELECT column AS alias_name FROM table AS t;
example
SELECT
  u.first_name AS name,
  o.total_amount AS order_total
FROM users AS u
JOIN orders AS o ON o.user_id = u.id;
output
-- name  | order_total
-- Alice | 149.99

Note AS is optional in most databases but improves clarity. You cannot reference a column alias in the WHERE clause of the same query — use a subquery or CTE instead.

Computed Columns

syntax
SELECT expression AS alias FROM table;
example
SELECT
  product_name,
  price,
  quantity,
  price * quantity AS line_total
FROM order_items;
output
-- product_name | price | quantity | line_total
-- Widget       | 25.00 | 3        | 75.00

Note You can use arithmetic operators (+, -, *, /), string functions, and CASE expressions directly in the SELECT list.

SELECT INTO / CREATE TABLE AS

syntax
-- PostgreSQL / SQL Server
SELECT columns INTO new_table FROM source_table;
-- MySQL
CREATE TABLE new_table AS SELECT columns FROM source_table;
example
CREATE TABLE active_users AS
SELECT id, first_name, email
FROM users
WHERE is_active = true;
output
-- Creates a new table active_users with matching rows

Note This copies data but not indexes or constraints. MySQL uses CREATE TABLE ... AS SELECT, while PostgreSQL and SQL Server support SELECT ... INTO.

FETCH FIRST (ANSI Standard)

syntax
SELECT columns FROM table
ORDER BY column
FETCH FIRST n ROWS ONLY;
example
SELECT product_name, price
FROM products
ORDER BY price DESC
OFFSET 5 ROWS
FETCH FIRST 10 ROWS ONLY;
output
-- Skips 5, returns next 10 most expensive products

Note FETCH FIRST is the ANSI SQL standard way to limit results. PostgreSQL supports both FETCH FIRST and LIMIT. MySQL only supports LIMIT. Use FETCH FIRST for maximum portability.