SELECT and FROM

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 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?

Options
• 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.

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 — 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.

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)

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.

Starter code
-- Write your query below
Back to the course