Back to subject
MySQL/SELECT

Ask for Exactly What You Need

Practical lesson

Let's compare SELECT * with a more targeted SELECT and see why the difference matters.

SELECT name, age FROM students; asks for exactly two columns, for every row in the table. Compare that to SELECT * FROM students;, which would also return the id column useful sometimes, but unnecessary if all you actually need is name and age.

The result table's column headers (name, age) come directly from the columns you named, in the exact order you named them SELECT age, name FROM students; would return the same data with the columns swapped.

Every row in students that exists gets included here, because there's no filtering yet you'll add that with WHERE in the next topic, which lets you return only rows matching a condition instead of everything.

For now, get comfortable with the core shape: SELECT [columns] FROM [table];. Almost every query you'll ever write, no matter how advanced, builds on top of this exact same foundation.

Try it yourself
Code

Starter example

Code
SELECT name, age FROM students;
Live preview
+------+-----+
| name | age |
+------+-----+
| Ali  | 16  |
| Sara | 17  |
+------+-----+
2 rows in set