How to use the SELECT statement in SQL
· Category: SQL & Databases
Short answer
SELECT retrieves columns from a table. Specify columns or use * for all columns, then name the table with FROM.
Steps
- Select all columns:
SELECT * FROM employees; - Select specific columns:
SELECT first_name, last_name FROM employees; - Use expressions:
SELECT first_name, salary * 1.1 AS new_salary FROM employees; - Eliminate duplicates with
SELECT DISTINCT department FROM employees; - Alias tables:
SELECT e.first_name FROM employees AS e;
Tips
- Avoid
SELECT *in production queries; name only the columns you need to reduce I/O and network overhead. - Use meaningful column aliases to improve readability in result sets and reporting tools.
Common issues
- Forgetting
FROMcauses a syntax error in most SQL dialects. - Column name typos produce
Unknown columnerrors instead of returning nulls.r