Your First SQL Query
After this lesson, you will be able to:
- Explain what a relational database is and why SQL is still the standard way to question one
- Identify the three building blocks of a relational database: tables, rows, and columns
- Write a SELECT * FROM table query to retrieve all data from a table
- Use SELECT with specific column names to retrieve only the data you need
- Apply LIMIT to control how many rows you get back
- Read and understand query results displayed in a table format
- Explain why SQL is still the universal language of data 50 years after Codd invented it
#What Is a Database?
You are about to learn the most in-demand data skill on the planet. SQL has been around since the 1970s and is more relevant today than ever. Every data scientist, ML engineer, and backend developer uses it daily. You have got this.
- It can hold millions (or billions) of rows without slowing down
- Multiple people can read and write data at the same time
- It enforces rules so bad data cannot sneak in
- It can find exactly what you need in milliseconds
#Tables, Rows, and Columns
employees table looks like:| id | name | department | salary | hire_date |
|---|---|---|---|---|
| 1 | Alice Johnson | Engineering | 95000 | 2020-01-15 |
| 2 | Bob Smith | Engineering | 85000 | 2020-06-01 |
| 3 | Carol Davis | Marketing | 78000 | 2019-03-20 |
| 4 | David Wilson | Engineering | 92000 | 2021-02-10 |
| 5 | Eve Brown | Marketing | 72000 | 2021-08-15 |
- Table = the entire thing (like one tab in a spreadsheet). This one is called
employees - Row = one horizontal line of data (one employee). Also called a record
- Column = one vertical category of data (like
nameorsalary). Also called a field - Cell = one specific piece of data (like "Alice Johnson" or 95000)
#Your Very First Query — SELECT *
SELECT, which means "give me data."The simplest possible query:
SELECT * FROM employees;
Let us break this down word by word:
SELECT— "I want to retrieve data"*— "give me ALL columns" (the asterisk means "everything")FROM employees— "from the table called employees";— the semicolon marks the end of the query (like a period at the end of a sentence)
You met the star in SQL: Start Here. Here it is your quick way to look at a whole table.
Try it! Click "Run" in the playground below. You just queried a database.
Every row in the result is one employee, and every column is one piece of information about them.
What does SELECT * FROM orders; return?
#Selecting Specific Columns
* is great for exploring, but in real work you usually only need certain columns. Instead of *, list the column names you want:SELECT name, department FROM employees;
name and department columns — nothing else. The result looks like:| name | department |
|---|---|
| Alice Johnson | Engineering |
| Bob Smith | Engineering |
| Carol Davis | Marketing |
| ... | ... |
SELECT *?- Speed. Fetching 2 columns is faster than fetching 10, especially with millions of rows
- Clarity. You see exactly the data you need, nothing extra
- Best practice. In production code,
SELECT *is considered lazy. Always name your columns
You can select as many columns as you want, separated by commas:
SELECT name, salary, hire_date FROM employees;
Try it! Modify the query below to select onlynameandsalary. Then try addingdepartmentas a third column.
#Column Order
The columns appear in the order you list them, not the order they exist in the table:
SELECT salary, name FROM employees;
salary first, then name — even though in the original table, name comes before salary. You control the output.#LIMIT — Controlling How Many Rows You Get
LIMIT cut a hand-traced answer in SQL: Start Here. What is new here is how analysts use it: as a first peek at a table too big to print.SELECT * FROM mystery_table LIMIT 10 to see what columns exist and whether the data is what you expected.-- Combine with specific columns
SELECT name, salary FROM employees LIMIT 3;
Try it! Change the LIMIT number in the playground below. Try LIMIT 1, LIMIT 5, and LIMIT 100 (what happens when you request more rows than exist?).
LIMIT 100 but the table only has 10 rows, you get all 10 rows. No error. The database just gives you everything it has, up to your limit.What does SELECT name FROM employees LIMIT 2; return?
#SQL Is Not Case-Sensitive (But Conventions Matter)
SQL keywords are case-insensitive. All of these are identical:
SELECT * FROM employees;
select * from employees;
Select * From Employees;
SeLeCt * fRoM eMpLoYeEs;
They all work. But the convention is:
- SQL keywords in UPPERCASE:
SELECT,FROM,WHERE,LIMIT - Table and column names in lowercase:
employees,name,salary
This makes queries easier to read at a glance. You can instantly see which words are SQL commands and which are your data.
-- Good style (recommended)
SELECT name, salary FROM employees LIMIT 10;
-- Works but harder to read
select name, salary from employees limit 10;
#Multiple Tables in One Database
A real database has many tables. The sample database in this playground has three:
- employees. People who work at the company
- orders. Purchases made by customers
- products — items for sale
Each table stores a different kind of data. You query them the same way:
-- See all orders
SELECT * FROM orders;
-- See all products
SELECT * FROM products;
-- Peek at the orders table
SELECT customer_name, product, amount FROM orders LIMIT 5;
Try it! Explore all three tables below. Start withSELECT * FROM orders;, then trySELECT * FROM products;.
Later, you will learn how to combine data from multiple tables using JOINs. For now, just know that each table holds one type of data, and you can query any of them.
#Comments in SQL
Like any language, SQL lets you write comments — notes for yourself (or future you) that the database ignores:
-- This is a single-line comment. Everything after -- is ignored.
SELECT name, salary -- you can put comments at the end of a line too
FROM employees;
/* This is a
multi-line comment.
Useful for longer explanations. */
SELECT * FROM products;
Comments are your friend. Use them to explain WHY you wrote a query a certain way, especially for complex queries.
#Practice Playground
Time to practice everything you have learned. Use the playground below to try these challenges:
- Select all columns from the
productstable - Select only
nameandpricefromproducts - Get the first 3 orders (all columns)
- Select
customer_nameandamountfromorders, limited to 5 rows - Select
name,department, andsalaryfromemployees
#Key Takeaways
- A database is a collection of tables, each holding a specific type of data. Tables have rows (records) and columns (fields)
- SELECT is the fundamental SQL command — it retrieves data from a table.
SELECT *gets all columns,SELECT name, salarygets specific columns - FROM specifies which table to query. Every SELECT needs a FROM
- LIMIT controls how many rows you get back. Essential for previewing large datasets (in BigQuery/Snowflake,
LIMITalso caps bytes scanned — saves real money) - SQL keywords are case-insensitive but the convention is UPPERCASE keywords, lowercase table/column names
- Comments use -- for single-line and /* */ for multi-line. Use them generously
What does the * mean in SELECT * FROM employees?