SELECT and FROM
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — The SELECT Statement
The SELECT statement is used to retrieve data from a database. It's the most commonly used command in SQL.
The basic syntax of a SELECT statement is:
SELECT column1, column2, ...
FROM table_name;Let's look at an example using a table of Codémons:
SELECT name, type FROM codemon;This query will return just the name and type columns for all Codémons in the database.
Exercise 2 — Selecting All Columns with *
If you want to select all columns from a table, you can use the asterisk (*) wildcard instead of listing each column name.
SELECT * FROM table_name;Using the Codémon example:
SELECT * FROM codemon;This will return all columns (id, name, type, and power) for all Codémons.
When to use SELECT *
While SELECT * is convenient for exploration and learning, in real production databases it's usually better to select only the specific columns you need. This improves query performance and reduces network traffic.
Exercise 3 — Correct SELECT Syntax
Which of the following is the correct syntax for retrieving all data from the codemon table?
SELECT * FROM codemon;RETRIEVE * FROM codemon;SELECT ALL FROM codemon;SELECT codemon.*;Hint : The correct syntax uses SELECT followed by what you want to select (columns or *) and then FROM followed by the table name.
Exercise 4 — Complete the Query
Complete the SQL query to retrieve only the name and power columns from the codemon table.
Hint : The basic structure of a query is SELECT followed by the columns you want, then FROM followed by the table name.
Exercise 5 — SQL Terms and Meanings
Match each SQL term with its correct description.
SELECTFROM*;Exercise 6 — Column Order in SELECT
The order of columns in your SELECT statement determines the order they appear in your results.
SELECT name, type, power FROM codemon;Now let's change the column order:
SELECT power, name, type FROM codemon;Notice how the column order in the results matches the order in your SELECT statement, regardless of how the table was originally created.
Exercise 7 — Predict the Output
Given this Codémon table:
What will be the output of the following query?
SELECT type, name FROM codemon;type, namename, typeid, name, type, powerHint : The columns in the result will appear in the exact order you list them in the SELECT statement.
Exercise 8 — Select Everything
Can you select all columns from the codemon table?
Hint : The correct syntax for retrieving all columns is SELECT * FROM table_name; - Don't forget the semicolon at the end!
Exercise 9 — Match Queries with Results
Match each SQL query with its expected result from the codemon table.
SELECT name FROM codemon;SELECT * FROM codemon;SELECT power, name FROM codemon;SELECT name, type, power FROM codemon;Exercise 10 — Your first query
Time to write a query on your own! Professor Pi wants a quick list of the Codémons in the lab.
Write a query that returns the name and the power of every Codémon in the codemon table.
-- Write your query below