The SELECT Statement

Introduction to the SELECT Statement

Asking the Database Questions

The SELECT statement is how you read data from a database. It doesn't change anything; it just retrieves information for you to view.

Every query needs two core components:

Welcome to your first step in mastering SQL! To get information from a database, we use the SELECT statement. Think of SELECT as choosing which columns you want to see, and FROM as telling the database which table to look in. It is like ordering a specific dish from a specific menu.

The Power of SELECT

The Foundation of SQL

The SELECT statement is the most fundamental command in SQL. It is used to retrieve data from a database, allowing you to ask questions and view specific information stored in tables.

SQL is a declarative language: you tell the database what you want, not how to get it.

Welcome to your first step in querying databases. To talk to a database, we use the SELECT statement. Think of it as asking the database a specific question to get back a specific set of answers. Unlike other programming languages, you don't tell SQL how to search; you simply declare what data you need.

Retrieving All Columns: The Wildcard

Using the Asterisk (*)

To see every column in a table, use the * symbol, known as the wildcard.

SELECT *
FROM Movie;

This is perfect for exploring a new table to see what data it contains.

Sometimes you want to see everything at once. By using the asterisk symbol—the wildcard—you tell SQL to grab every single column. When we run 'SELECT star FROM Movie', the database returns all columns like ID, Title, and Genre for every movie in the list.

Retrieving All Columns

Using the Wildcard (*)

If you want to see every column in a table, use the asterisk (*) as a wildcard. This is helpful for exploring a new table.

Syntax:
SELECT * FROM Movie;

Imagine you want to see every piece of information in our Movie table. We use the asterisk wildcard after the SELECT keyword. This tells the database to return every column: ID, Title, Genre, RatingCode, and Year.

Narrowing Your Focus

Retrieving Specific Columns

In professional analysis, you usually only need specific columns. List the exact column names you want, separated by commas.

Example:
SELECT Title, Year FROM Movie;

Often, seeing every column is overwhelming. If we only care about movie titles and their release years, we list them specifically. Notice how the other columns disappear, leaving us with a clean, focused result.

Selecting Specific Columns

Refining Your Results

In professional work, you usually only need specific columns. List them by name, separated by commas.

SELECT Title, Year
FROM Movie;

This makes your query faster and your results easier to read.

You don't always need the whole table. If you only care about movie names and their release dates, you simply list 'Title' and 'Year' after the SELECT keyword. Notice how the resulting table is much cleaner now, showing only the two columns we requested.

Bug Hunter: SQL Syntax

SQL is strict! Click on the syntax errors in the queries below to fix them.

Even small mistakes can stop a query from running. Look at these three examples and click on the errors you see. Great catch! You need a comma between Title and Year. Without it, the database gets confused. Correct. The table is named 'Movie', not 'Movies'. SQL is very literal about spelling. Exactly. The semicolon is like a period at the end of a sentence—it tells the database the command is finished.

Practice: Building a Query

Help the studio find their movies and genres. Drag the correct keywords into the blanks to complete the query.

____ Title, Genre ____ Movie;

Let's try it out! You need to retrieve the 'Title' and 'Genre' columns from the 'Movie' table. Drag the SELECT and FROM keywords into the correct spots to finish the statement. Perfect! You've correctly structured the query. Remember: SELECT columns, FROM table, and end with a semicolon.

Query Challenge: Title and Genre

Write a query to retrieve the Title and Genre columns from the Movie table. Remember to use uppercase for keywords and end with a semicolon.

It's time to write your own code. Type a query that retrieves just the Title and Genre columns from the Movie table.

Common Pitfalls to Avoid

Watch Out for These Errors!

Even pros make mistakes! Forgetting a comma between 'Title' and 'Year' is the number one cause of errors for beginners. Also, watch out for 'FORM' instead of 'FROM'. Finally, always double-check your table name. If the table is named 'Movie', writing 'Movies' with an 'S' will result in an error.

Final Challenge: Write Your Query

Write a query to retrieve all information from the Movie table. Remember your syntax rules!

It's time for your final challenge. Write a complete SQL query in the editor to see every column in the Movie table. Click submit when you are done.