Sql · Basic SQL Queries
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 a table named "trainers"?
Options
• ```SELECT * FROM trainers;```
• ```RETRIEVE * FROM trainers;```
• ```SELECT ALL FROM trainers;```
• ```SELECT trainers.*;```
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.
____ name, power ____ codemon;
Options: SELECT RETRIEVE FROM IN TABLE WHERE
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.
Items to match
• SELECT
• FROM
• *
• ;
Definitions
• Keyword used to retrieve data
• Specifies which table to query
• Wildcard meaning all columns
• Ends an SQL statement
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;
Options
• A table with columns in order: type, name
• A table with columns in order: name, type
• A table with all columns: id, name, type, power
• An error because the columns are in the wrong order
Hint : The columns in the result will appear in the exact order you list them in the SELECT statement.
Exercise 8 — Fix the Query
Can you select all columns from the trainers table?
____ ____ ____ trainers____
Options: SELECT SHOW * ALL FROM IN ;
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.
Items to match
• SELECT name FROM codemon;
• SELECT * FROM codemon;
• SELECT power, name FROM codemon;
• SELECT name, type, power FROM codemon;
Definitions
• Only Codémon names
• All columns and all rows
• Power values first, then names
• Name, type, and power columns (no id)
Back to the course