So, we've already started digging into SELECT, the SQL command that's used to pull data from a table or even a bunch of tables in your database. You could say it's like hitting F5 in your browser, but for your database: "show me what you've got." The basic query structure looks like this:
SELECT column1, column2, … columnN
FROM table
Where:
SELECT— this is the keyword your query starts with.column1, column2, ...— this is the list of columns you want to get data from.FROM table— tells SQL which table to pull the data from.
Let's break this down right away with some examples!
Main Structure of the SELECT Command
Selecting all columns. If you want to see all the data from a table, you can use the asterisk *. For example, let's say we have a table called students:
| id | name | age | grade |
|---|---|---|---|
| 1 | Alex | 17 | A |
| 2 | Maria | 19 | B |
| 3 | Otto | 21 | C |
To pull all the data from this table, use this query:
SELECT * FROM students
And the result will be this set of columns and rows:
| id | name | age | grade |
|---|---|---|---|
| 1 | Alex | 17 | A |
| 2 | Maria | 19 | B |
| 3 | Otto | 21 | C |
So — that's the whole table, just like you asked for.
Selecting Specific Columns
Of course, most of the time you only want to grab the columns you actually need, so you don't drag around extra data. For example, let's say you want to see the names and ages of the students. Then you should write a query like this:
SELECT name, age FROM students
The result of this query will be:
| name | age |
|---|---|
| Alex | 17 |
| Maria | 19 |
| Otto | 21 |
See the difference? When you only pick the columns you need, your query is faster and cleaner.
Query Execution Order
SQL queries can be a little sneaky: even though we write SELECT at the start, the FROM part actually gets processed first, and only then do you get your result. Here’s what’s going on under the hood:
- The table in
FROMis processed: the server finds the table you mentioned. - Any conditions are applied to the rows (if there are any): only the rows that match the conditions are picked.
- The specified columns are selected: from the remaining rows, the data you asked for is pulled out.
So in a command like this:
SELECT name FROM students
First, the server finds the students table, then reads the name column, and only after that returns the result.
Practical Examples
Example #1: List of students.
Imagine you're a university admin and you need the names of all the students. The query:
SELECT name
FROM students;
Result:
| name |
|---|
| Alex |
| Maria |
| Otto |
Example #2: Add age and id.
Now you want the id, names, and ages. Just add a couple more columns:
SELECT name, age, id
FROM students;
Result:
| name | age | id |
|---|---|---|
| Alex | 17 | 1 |
| Maria | 19 | 2 |
| Otto | 21 | 3 |
Example #3: Add expressions.
Let's say you want to know how many years your students have left until they hit adulthood.
Then instead of age, you’d write 21-age. Here, just for fun, we’ll say adulthood is at 21:
SELECT name, 21-age
FROM students;
And here’s what you’ll get:
| name | 21-age |
|---|---|
| Alex | 4 |
| Maria | 2 |
| Otto | 0 |
You’ll learn more about expressions in the next lectures. But now, let’s check out some mistakes that some of you have probably already run into.
Common Mistakes When Using SELECT
Typos in table and column names.
One of the most common mistakes is when you type the table or column name wrong.
If there’s no table called studentsz in your database, you’ll get an error:
SELECT name
FROM studentsz; -- Error: table doesn't exist!
Or if the column is called name, but you wrote student_name, again, error:
SELECT student_name -- Error: column doesn't exist!
FROM students;
The fix is simple: always double-check your table and column names.
Syntax errors in the query.
Another common mistake — you forgot a comma when listing columns:
SELECT name age id -- Error: missing comma!
FROM students;
But don’t worry. The SELECT query only pulls data. Any mistakes in this kind of query can’t mess up the data in your database. But when we get to queries that actually change data — then mistakes will cost you a lot more.
Using * instead of specific columns.
When you use *, you’re pulling ALL the data, including stuff you don’t need. That slows things down and can be a problem with big tables. Always pick just the columns you really need.
GO TO FULL VERSION