Basic English simplifies SQL concepts - Lesson 2
If you speak basic English, writing SQL becomes effortless
👋🏽 Hey, it’s Ismail. Welcome to data nomads lab newsletter on learning data analytics, career growth, networking, building portfolios, and interview skills to break into tech role as a high-performer.
SELECT these columns or all columns FROM this table WHERE this condition is true or false but I don’t want to see all thousands or millions of results, just LIMIT is to first 10, 20, 50 or 100 data records.
But I want you to ORDER BY the results from smallest to largest (A to Z) aka Ascending (ASC) or from largest to smallest Descending (DESC).
Since we spend a lot of money on music for different countries, I want to GROUP BY the total amount we spent in each country for music.
You can further refine your query to be more precise and adaptable to different conditions by using SQL command
IN: to check is a value exist in a specific list
LIKE: to search for patterns in text
BETWEEN: to filter values within a range
NOT: to exclude specific values or conditions
If you are still confused do not stress — we will use the Chinook database to explain everything above in examples that is easy to follow.
SELECT and FROM: the core of SQL
Every SQL query begins with
SELECT
andFROM
. Think of these as the what and where of your query.
Example
Imagine you are looking at the Tracks table and want to list all track names:
SELECT Name
FROM Tracks;
This query tells SQL: Find the Name column in the Tracks table
Output (partial)
Name
----------------
For Those About to Rock
Put The Finger On You
Let's Get It Up
...
By default, SQL returns every row in the table—whether there are 10 or 10,000 rows.
SELECT * : Retrieving All Data
In SQL, the asterisk (*) is a wildcard that represents all columns in a table. When you want to select every column from a table, you simply use SELECT *
.
Example
Suppose you want to view every piece of information about tracks from the Tracks table:
SELECT * FROM Tracks;
This query returns every column and every row from the Tracks table.
WHERE: Filtering Your Data
The
WHERE
clause is your filter. It helps you narrow down the results to only those that meet specific conditions.
Example
Suppose you want tracks with a Milliseconds
value greater than 300,000 (about 5 minutes):
SELECT Name, Milliseconds
FROM Tracks
WHERE Milliseconds > 300000;
This query returns only tracks longer than 5 minutes.
LIMIT: Keep It Short and Sweet
Sometimes, you don’t need all the data.
LIMIT
helps you control how many rows are returned.
Example
Let’s retrieve the first 5 tracks:
SELECT Name
FROM Tracks
LIMIT 5;
Output
Name
----------------
For Those About to Rock
Put The Finger On You
Let's Get It Up
Inject the Venom
Snowballed
ORDER BY: Sorting Matters
ORDER BY
arranges your data. By default, it sorts in ascending order, but you can specifyDESC
for descending.
Example
List the top 5 longest tracks:
SELECT Name, Milliseconds
FROM Tracks
ORDER BY Milliseconds DESC
LIMIT 5;
GROUP BY: Aggregating Your Data
GROUP BY
lets you summarize data. Pair it with aggregate functions likeCOUNT
,SUM
, orAVG
.
Example
Count how many tracks each album contains:
SELECT AlbumId, COUNT(*) AS TrackCount
FROM Tracks
GROUP BY AlbumId;
IN and NOT: Specifying Multiple Values
Use
IN
to filter data based on a list of values. AddNOT
to exclude those values.
Example
Find all tracks from artists with IDs 1, 3, or 5:
SELECT Name
FROM Tracks
WHERE ArtistId IN (1, 3, 5);
BETWEEN: Working with Ranges
BETWEEN
is ideal for specifying ranges, whether dates, numbers, or strings.
Example
Find tracks with Milliseconds
between 200,000 and 300,000:
SELECT Name, Milliseconds
FROM Tracks
WHERE Milliseconds BETWEEN 200000 AND 300000;
NULL: Handling Missing Values
In SQL,
NULL
represents missing data. UseIS NULL
orIS NOT NULL
to filter for or exclude these rows.
Example
Find tracks with missing composer information:
SELECT Name
FROM Tracks
WHERE Composer IS NULL;
Wrapping Up
Mastering these foundational SQL commands, SELECT
, FROM
, WHERE
, LIMIT
, ORDER BY
, GROUP BY
, IN
, NOT
, BETWEEN
, and NULL
is your first step to becoming a confident data analyst. Each command is like a puzzle piece that helps you construct meaningful queries.