SELECT – Retrieving Data
The SELECT
statement is the foundation of retrieving data from a relational database. Below are common patterns and examples.
1. Basic SELECT
SELECT * FROM Employees;
2. Selecting Specific Columns
SELECT FirstName, LastName, HireDate
FROM Employees;
3. Using WHERE to Filter Rows
SELECT FirstName, Salary
FROM Employees
WHERE Salary > 50000;
4. Ordering Results
SELECT FirstName, HireDate
FROM Employees
ORDER BY HireDate DESC;
5. Limiting Rows (TOP / LIMIT)
-- SQL Server
SELECT TOP 5 * FROM Employees;
-- MySQL / PostgreSQL
SELECT * FROM Employees LIMIT 5;
6. Column & Table Aliases
SELECT e.FirstName AS Name, d.Name AS Department
FROM Employees e
JOIN Departments d ON e.DeptID = d.ID;