The SELECT statement is the bread and butter of SQL. If you learn nothing else,
learn this. It is how you get data out of a database.
Selecting Columns
You do not always need to grab every column with *. Most of the time, you
only want specific columns:
SELECT name, age FROM students;
This will return only the name and age columns for every row.
It is faster and cleaner than grabbing everything.
Column Aliases
Sometimes you want the column name in the result to be different from what it is called
in the table. You can do that with AS:
SELECT name AS student_name, score AS exam_score FROM students;
This does not change the actual table. It just changes how the column name appears in the results. Handy when you are building reports or working with applications that expect specific column names.
Selecting Expressions
You can also do calculations directly in SELECT:
SELECT name, score, score + 5 AS bonus_score FROM students;
This adds 5 to every student's score. The original data stays the same โ SQL is just showing you the calculated result.
Concatenating Text
You can combine text from different columns. In SQLite, we use ||:
SELECT name || ' is ' || age || ' years old' AS description FROM students;
This creates a nice readable sentence from your data.