Introduction to SELECT
The SELECT
statement is the most commonly used SQL command. It allows you to retrieve specific data from one or more tables.
If you want to look at the data you've stored in a table, like student scores or exam results, you'll use SELECT
. Whether it's a student's name, a list of passed students, or just the top scores, SELECT
helps you fetch exactly what you need.
Basic SELECT Syntax
The example below shows how to retrieve data from a table using the SELECT
statement:
SELECT column1, column2
FROM table_name;
SELECT
specifies which columns to retrieve.FROM
specifies which table to retrieve the data from.
Select All Columns
The *
is a wildcard that means "all columns".
SELECT * FROM mid_exam;
This query retrieves every column from the mid_exam
table.
Example: View Names and Exam Scores
Let's say you have a table called mid_exam
:
id | name | exam_score | passed |
---|---|---|---|
1 | John Miller | 78 | Yes |
2 | Emily Davis | 91 | Yes |
3 | Michael Johnson | 84 | Yes |
4 | Sophia Wilson | 65 | No |
5 | Ethan Brown | 88 | Yes |
You can retrieve the names and exam scores using:
SELECT name, exam_score
FROM mid_exam;
This returns:
name | exam_score |
---|---|
John Miller | 78 |
Emily Davis | 91 |
Michael Johnson | 84 |
Sophia Wilson | 65 |
Ethan Brown | 88 |
Bonus: View Name and Passed Status
If you want to check who passed:
SELECT name, passed
FROM mid_exam;
This query shows whether each student passed the midterm.
Why select matters
The SELECT
command is the starting point for exploring and analyzing data. Once you've mastered it, you'll move on to:
- Filtering rows using
WHERE
- Sorting using
ORDER BY
- Combining multiple tables with
JOIN
All these skills begin with understanding how SELECT
works.