SQL SELECT
Queries
Learn to write SELECT statements that retrieve exactly the columns you need from a database: the most fundamental skill in SQL.
What Is a Database?
A database is an organized collection of data stored in tables. Think of it like a spreadsheet: each table has rows (records) and columns (fields). SQL is the language we use to talk to databases and ask them questions.
What Does SELECT Do?
SELECT is the SQL keyword that retrieves data from a database. It lets you choose which columns to see and which table to pull them from. Every data question you ask starts with SELECT.
Why SELECT Matters
Real databases contain millions of rows and dozens of columns. Retrieving only the columns you need makes queries faster, results clearer, and your work more professional.
The SELECT Syntax
SELECT * vs Specific Columns
The asterisk * is a wildcard meaning "all columns." It is useful for quick exploration but should be avoided in production queries.
Keywords vs Identifiers
SQL has two kinds of words in every query. Understanding the difference prevents errors and confusion.
| Keywords | Identifiers | |
|---|---|---|
| Definition | Reserved words built into SQL | Names you (or the designer) create |
| Examples | SELECT, FROM, WHERE | students, name, grade |
| Case | Conventionally UPPERCASE | Conventionally lowercase |
| Can you rename? | No | Yes (with aliases) |
How to Write a SELECT Query
Example 1: One Column
Question: Get the names of all students.
Example 2: Multiple Columns
Question: Get the name and grade of every student.
| name | grade |
|---|---|
| Alice | 10 |
| Bob | 11 |
| Charlie | 10 |
| Diana | 12 |
Example 3: SELECT * in Action
Question: See every column and every row of the students table.
| id | name | grade | |
|---|---|---|---|
| 1 | Alice | 10 | alice@school.edu |
| 2 | Bob | 11 | bob@school.edu |
| 3 | Charlie | 10 | charlie@school.edu |
| 4 | Diana | 12 | diana@school.edu |
Column Order Matters
The order you list columns after SELECT controls the order they appear in the result. This is useful for presenting data clearly.
| grade | name |
|---|---|
| 10 | Alice |
| 11 | Bob |
| 10 | Charlie |
| 12 | Diana |
Renaming with AS
You can give a column a temporary display name using the AS keyword. This is called an alias. It does not change the actual table: it only changes how the result looks.
| student_name | year_level |
|---|---|
| Alice | 10 |
| Bob | 11 |
| Charlie | 10 |
| Diana | 12 |
Removing Duplicates with DISTINCT
When a column has repeated values, SELECT DISTINCT returns only unique entries.
Common Mistakes
SELECT Query Cheat Sheet
| Pattern | Syntax | Use When |
|---|---|---|
| All columns | SELECT * FROM t; | Quick exploration |
| Specific columns | SELECT a, b FROM t; | You know what you need |
| Renamed columns | SELECT a AS x FROM t; | Clearer output labels |
| Unique values | SELECT DISTINCT a FROM t; | Remove duplicate rows |
Question 1 of 5
Which SQL keyword is used to retrieve data from a table?
Question 2 of 5
What does SELECT * return?
Question 3 of 5
Given a table called books, which query retrieves only the title and author columns?
Question 4 of 5
What does the AS keyword do in a SELECT query?
Question 5 of 5
If the employees table has a department column with values: Sales, Engineering, Sales, HR, Engineering, Sales: how many rows does this query return?