Welcome to your first SQL lesson! To retrieve data from a SQL database, we use SELECT
statements, which are commonly referred to as queries. A query is simply a request that tells the database what data we need, where to find it, and optionally, how to process it before returning the results.
Think of a query as asking a question to your database: "Show me all the customer names" or "What are the top 5 highest-selling products?" The database then responds with the exact information you requested.
🗃️ Understanding Tables and Data
Before we write our first query, let's understand how data is organized in SQL databases.
You can think of a SQL table as representing a type of entity. For example:
- A table named
Dogs
could store information about different breeds of dogs - A table named
Movies
could store information about films - A table named
Customers
could store information about your business customers
Table structure:
- Rows represent specific instances of that entity (e.g., a pug, a beagle, a golden retriever)
- Columns define the attributes shared by all instances (e.g., fur color, tail length, breed)
📝 Writing Your First SQL Query
The simplest query we can write is one that selects specific columns from a table while including all rows. This allows us to retrieve only the information we need without pulling unnecessary data.
🎯 Selecting Specific Columns
SELECT column_name, another_column
FROM table_1;
This query extracts only the specified columns from table_1
and returns the data as a structured result with rows and columns.
Example:
SELECT title, director
FROM movies;
This would return just the movie titles and directors, without other information like release year, budget, or ratings.
🌟 Selecting All Columns
If you want to retrieve all columns from a table without listing each one individually, you can use the *
wildcard:
SELECT *
FROM table_1;
This query returns every column and row from the table, making it a quick way to inspect data.
💡 Important Note: In large databases, using SELECT *
can be inefficient because it retrieves all data, even columns you don't need. It's generally recommended to specify only the columns you actually need for better performance.
🔍 When to Use Each Approach
Use specific column selection when:
- You only need certain pieces of information
- Working with large datasets (better performance)
- Creating reports or displays with specific formatting
- You want to reduce data transfer and processing time
Use SELECT *
when:
- Exploring a new table to see what data is available
- Working with small datasets
- You genuinely need all the information from the table
- Doing quick data inspection or debugging
✅ Key Takeaways
- A query retrieves data based on specific conditions and requirements
- Tables store data in a structured format with columns (attributes) and rows (records)
- The SELECT statement allows you to fetch specific columns or all columns from a table
- Specific column selection is more efficient than using
SELECT *
in most cases - Use
SELECT *
wisely - it's great for exploration but can be inefficient with large datasets - SQL syntax is structured and follows predictable patterns that make it easy to learn
📝 Exercise Overview
Throughout these lessons, you'll be working with a sample database containing data about Oscar-winning movies. This familiar and fun dataset will help you stay focused on learning SQL without being distracted by complex domain-specific data.
In this first exercise, you'll be working exclusively with the oscar_movies
table. As the lessons progress, you'll explore more tables and relationships between them.
Each lesson includes 5–6 hands-on tasks designed to reinforce the concept just covered. These tasks will give you both practical coding experience and a better understanding of how SQL works in real-world scenarios.
Take your time with each one—experiment, break things, and try variations. That's one of the best ways to learn!