Introduction
The SELECT statement is the most fundamental command in SQL. It is used to read and retrieve data from one or more tables in a relational database. Every report, dashboard, or application query that displays data ultimately relies on a SELECT statement.
Cricket analogy: Just as a scorecard is the base document every commentary and highlight reel is built from, SELECT is the base command every dashboard and report in an application ultimately relies on to pull data.
Syntax
SELECT column1, column2, ...
FROM table_name;
-- To select all columns
SELECT *
FROM table_name;Explanation
The SELECT clause lists the columns you want returned, separated by commas. The FROM clause specifies which table to read from. Using an asterisk (*) instead of column names returns every column in the table, which is convenient for exploration but should be avoided in production code because it can return more data than needed and breaks if the table schema changes.
Cricket analogy: Calling for the full scorecard with every statistic (SELECT *) when you only need runs and wickets is wasteful, just as listing only first_name, last_name, department is more efficient than pulling every column.
Example
-- employees table: id, first_name, last_name, department, salary, hire_date
SELECT first_name, last_name, department
FROM employees;Output
The query returns a result set with three columns (first_name, last_name, department) and one row for every employee stored in the table, for example: 'Alice | Nguyen | Engineering', 'Ben | Osei | Marketing', and so on.
Cricket analogy: The result set listing every employee's name and department row by row is like a team sheet listing every player's name and role, one line per player, in the order the sheet was compiled.
Key Takeaways
- SELECT retrieves data; it does not modify the underlying table.
- Use SELECT * only for quick exploration, not in production queries.
- Column order in the result set matches the order listed after SELECT.
- You can compute expressions and aliases directly in the SELECT list, e.g. SELECT salary * 12 AS annual_salary.
Practice what you learned
1. Which keyword is used to specify which columns to retrieve in a SQL query?
2. What does SELECT * do?
3. Which clause specifies the table to query in a SELECT statement?
4. Why is it generally discouraged to use SELECT * in production code?
Was this page helpful?
You May Also Like
Filtering with WHERE
Use the WHERE clause to filter rows returned by a query based on specified conditions.
Sorting with ORDER BY
Control the order of query results using the ORDER BY clause with ascending or descending sorts.
Introduction to SQL
Get an overview of SQL, its main statement categories, and how it lets you define, query, and manipulate relational data.