Introduction
In relational database management systems (RDBMS) such as PostgreSQL, MySQL, SQL Server, SQLite, and Snowflake, data is stored in structured tables. The SELECT statement is the undisputed foundation of SQL: it is the primary command used to retrieve and read data from these tables.
Whether you are extracting monthly transactions for an e-commerce platform, pulling patient histories in a hospital application, analyzing customer churn, or building an executive reporting dashboard, every data workflow begins with SELECT.
SELECT is strictly a read-only query statement. Executing a SELECT query does not alter, insert, modify, or delete the underlying data stored inside your database. It simply reads the data and projects it into a virtual, formatted view called a Result Set.Understanding a SQL Table
Before writing SQL queries, you must understand how data is organized within a relational database. A database consists of one or more Tables, which are organized in a two-dimensional grid of rows and columns:
| Concept | Database Term | Description | Realistic Example (Customers Table) |
|---|---|---|---|
| Table | Entity / Relation | The entire 2D collection of organized records | customers table |
| Row | Record / Tuple | A single horizontal entry representing one entity | Customer ID 101: Rahul Sharma, Bangalore |
| Column | Field / Attribute | A vertical category with a specific data type | email, city, signup_date |
| Value | Cell / Data Point | The exact intersection of one row and one column | "Bangalore" or 75000 |
SQL queries allow you to request specific columns across all or filtered rows from this grid.
(5 columns, 10,000 rows)
(Column Projection Request)
(2 requested columns in memory)
Your First SELECT Query
The simplest SQL query contains two essential clauses: SELECT and FROM.
FROM customers;
Let us break down every element of this query:
SELECT: Tells the database query processor: "Extract and return the following column(s) in the result."name: The specific column name you want to retrieve.FROM: Specifies the source table where the data lives.customers: The exact table name in the database schema.;(Semicolon): The standard SQL statement terminator indicating the end of the query command.
Selecting Specific Columns
In real-world applications, tables often contain dozens of columns (e.g. metadata, system timestamps, encrypted hashes). You can select a single column or multiple columns by separating them with commas:
SELECT name
FROM customers;
-- Selecting multiple specific columns
SELECT name, email, city
FROM customers;
SELECT clause, regardless of how they are arranged in the original table definition.SELECT * (The Wildcard)
The asterisk (*) is known as the wildcard selector. It commands the SQL engine to return every single column available in the table in its default schema order:
FROM customers;
When SELECT * is Great | Why Avoid SELECT * in Production |
|---|---|
| ✅ Fast initial data exploration | ❌ Transfers unnecessary megabytes across the network |
| ✅ Inspecting newly created or unfamiliar tables | ❌ Consumes excessive application server memory |
| ✅ Quick interactive debugging in SQL client | ❌ Schema changes (adding/removing columns) can break downstream backend code |
Column Aliases (The AS Keyword)
Database column names are often terse or follow strict database naming conventions (such as usr_fnm, cust_addr_loc, or emp_sal_amt). You can use the AS keyword to give columns a user-friendly alias in the result set:
FROM customers;
Aliasing renames the column header in the returned result set. It does not change the permanent column name inside the database table.
Selecting Expressions & Calculated Values
A SELECT clause is not limited to returning static stored columns. You can evaluate mathematical expressions on the fly for each row.
SELECT product_name, price, quantity, price * quantity AS total
FROM products;
The SQL engine evaluates the expression price * quantity for every individual row and assigns the calculated value to the new virtual column labeled total.
FROM employeesSELECT name, salary * 12 AS annual[name, annual]SELECT DISTINCT — Basic Introduction
If multiple rows contain duplicate values in the selected columns, standard SELECT returns all of them. Adding the DISTINCT keyword instructs SQL to eliminate duplicate rows from the final result set:
FROM customers;
Mumbai
Delhi
Pune
Mumbai
Delhi
Pune
Understanding Result Sets
Whenever you run a query, the SQL engine constructs a Result Set. Think of a result set as a temporary, read-only table created in memory specifically to satisfy your query.
When you run SELECT DISTINCT department, city FROM employees;, SQL examines the combination of department and city across all rows, ensuring that no pair of (department, city) is repeated.
Reading and Understanding a SELECT Query
While humans read code from top to bottom, SQL query execution follows a specific logical order:
- Step 1 (FROM): Identify the table where the data is stored.
- Step 2 (SELECT): Select the columns, perform calculations, rename with aliases, and apply DISTINCT if requested.
- Step 3 (Output): Emit the formatted result set back to the client.
employees8 Total Rows| id | name | department | salary | city |
|---|---|---|---|---|
| 1 | Rahul Sharma | Engineering | ₹75,000 | Bangalore |
| 2 | Priya Patel | Marketing | ₹62,000 | Mumbai |
| 3 | Amit Verma | Engineering | ₹82,000 | Bangalore |
| 4 | Sneha Rao | HR | ₹55,000 | Pune |
| 5 | Vikram Singh | Marketing | ₹64,000 | Delhi |
| 6 | Ananya Gupta | Finance | ₹90,000 | Mumbai |
| 7 | Rohan Deshmukh | Engineering | ₹78,000 | Pune |
| 8 | Pooja Nair | HR | ₹57,000 | Bangalore |
Common Beginner Mistakes
Fix the query so that it retrieves both the name and department of every employee.
Practical SELECT Exercises
| Task Goal | Required Query | Output Concept |
|---|---|---|
| 1. Select One Column | SELECT email FROM customers; | Single-column projection |
| 2. Select Multiple Columns | SELECT name, department, salary FROM employees; | Comma-separated column list |
| 3. Rename Output Column | SELECT first_name AS customer_name FROM users; | Output header aliasing with AS |
| 4. Compute Derived Value | SELECT unit_price * quantity AS subtotal FROM order_items; | Row-by-row arithmetic expression |
| 5. Deduplicate Unique Values | SELECT DISTINCT country FROM suppliers; | Removes repeating country names |
SELECT Best Practices
- Capitalize SQL Keywords: Use uppercase for
SELECT,FROM,AS, andDISTINCTto clearly separate keywords from column identifiers. - Explicit Column Naming in Production: Avoid
SELECT *in production queries and APIs. Always request only the specific columns needed by the application. - Provide Meaningful Aliases: Whenever calculating expressions (e.g.
salary * 12), always assign a clean alias withAS annual_salaryso column headers remain clear. - Consistent Line Formatting: Break complex queries across multiple lines with
SELECTon one line andFROMon the next.
What You Should Know Now
You have mastered the foundation of SQL querying! Here is your core competencies checklist:
- ✓SELECT Keyword: Instructs SQL which columns to extract
- ✓FROM Clause: Identifies the source database table
- ✓Multiple Columns: Comma-separated column list
- ✓Wildcard (*): Selects all columns defined in table
- ✓Aliases (AS): Renames output headers in result set
- ✓Expressions: Evaluates math on the fly row-by-row
- ✓DISTINCT: Eliminates duplicate rows in the result
- ✓Result Sets: Temporary virtual output tables in memory
🎯 Knowledge Check Quiz: SQL SELECT
Test your understanding of SELECT syntax, result sets, expressions, aliases, and query predictions.