0% found this document useful (0 votes)
11 views98 pages

SQL Course (Datacamp) Databases _ SQL

Uploaded by

25110189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views98 pages

SQL Course (Datacamp) Databases _ SQL

Uploaded by

25110189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

%SQL DATACAMP + GPT NOTES

🔹 Introduction to Databases and SQL


🧠 Course Goals
●​ Goal 1: Understand what databases are and how they are structured to store data.​

●​ Goal 2: Learn how to interact with data using SQL (Structured Query Language).​

🔹 What is SQL?
●​ SQL = Structured Query Language.​

●​ It’s the most widely used language to talk to databases.​

●​ It allows us to:​

○​ Access data (e.g., view who checked out a book).​

○​ Organize data (e.g., sort books by title).​

○​ Analyze data (e.g., find the most borrowed book).​

●​ SQL works using commands called queries.​

🔹 What is a Database?
●​ A database is a system to store and organize data.​

●​ Example: A library database would store information like:​

○​ Books​

○​ Patrons (members)​

○​ Checkouts​

●​ A real-life example:​

○​ Think of a library system:​

■​ Track who joined the library.​

■​ What books they borrowed.​


■​ When the books are due.​

🔐 Why use databases (not just spreadsheets)?


●​ They can handle large amounts of data.​

●​ They offer better security (e.g., via encryption).​

●​ Multiple users can interact with the data at the same time.​

🔹 Understanding Tables
●​ A table is a basic building block of a database.​

●​ Think of it like a spreadsheet with rows and columns.​

📘 Example: Patrons Table


card_number name joined_year total_fines

1234 Alex 2020 5.00

5678 Sam 2019 0.00

●​ ​
Columns = types of data (e.g., name, joined_year).​

●​ Rows = one record per patron.​

🔹 Relational Databases
●​ A relational database = multiple connected tables.​

●​ They are related by common columns.​

●​ Example relationships:​

○​ checkouts.card_number → connects to patrons.card_number​

○​ checkouts.book_id → connects to books.book_id​

📘 Tables and Relationships


patrons books checkouts

card_number, name… book_id, title… book_id, card_number…

👉 This structure lets you ask smart questions, like:


“How many books has Izzy checked out?”

🔹 Why Use SQL Queries?


●​ SQL queries help us ask questions from the data.​

Example:​

SELECT name FROM patrons WHERE total_fines > 0;

●​ → This will show the names of patrons who owe fines.​

🧠 Key Point:
●​ When we query a database:​

○​ The data does NOT change.​

○​ We're simply accessing and presenting data as instructed.​

Thanks for sharing the next part! Here's the organized and beginner-friendly summary of this section
of the SQL course from DataCamp, focusing on tables, records, fields, and naming rules in databases.

🔹 Deep Dive: Tables in a Database


📌 1. Table Naming Conventions
●​ Table names should be:​

○​ Clear and meaningful (e.g., books, inventory, products)​

○​ Written in lowercase​

○​ Use underscores (_) instead of spaces​


library_books​
Library Books​

📌 2. Records and Fields


●​ Tables consist of:​

○​ Rows → also called records​

○​ Columns → also called fields​

🔍 Example
For a table called patrons:

card_num name joined_year total_fines

1001 Jasmin 2022 2.05

1002 Izzy 2021 0.00

●​ ​
Each row = 1 record (e.g., Jasmin's data)​

●​ Each column = 1 field (e.g., name or joined_year)​

📌 3. Understanding Records
●​ A record is a full set of data for one person or item.​

●​ Example:​

○​ Jasmin's record includes:​

■​ name = Jasmin​

■​ joined_year = 2022​
■​ total_fines = 2.05​

📌 4. Understanding Fields
●​ A field represents one type of data across all records.​

●​ Example:​

○​ The name field stores names of all patrons.​

○​ The total_fines field shows how much each patron owes.​

📌 5. Field Naming Conventions


●​ Same as table naming:​

○​ Use lowercase: card_num, not Card_Num​

○​ Use underscores: joined_year, not Joined Year​

○​ Use singular names:​

■​ ✅ name (refers to one person's name)​


■​ ❌ names (makes it sound like multiple names in one field)​
●​ Avoid:​

○​ Duplicate field names in the same table​

○​ Naming a field the same as the table name​

■​ If table = patrons, don’t use field name = patrons​

📌 6. Unique Identifiers (Keys)


●​ A unique identifier (key) is a field where:​

○​ Each value is unique​

○​ It helps identify each record​


●​ Example:​

○​ card_num = unique ID for library patrons​

○​ name is NOT a good key because different people might have the same name​

📌 7. Why Use Multiple Tables?


●​ Combining all data in one table:​

○​ Creates duplication (e.g., multiple rows for one person)​

○​ Makes it harder to manage​

●​ Relational design (separate tables linked by IDs) is better:​

○​ Keeps things organized and efficient​

○​ Makes queries easier and more powerful​

🔍 Example:
patrons checkouts

card_num, name… card_num, book_id, date…

●​ ​
Izzy can have multiple rows in checkouts, but only one row in patrons.​

●​ This keeps Izzy's info (e.g., fines, join date) from being repeated.​

📌 8. Practice Time
●​ You’ll now apply what you've learned:​

○​ Identify records and fields​

○​ Understand naming​

○​ See how tables relate to each other​


Here’s the final section of your SQL for Databases summary — neatly organized, clear for beginners,
and great for quick review or notes!

🗂️ Final Part: Database Storage & Data Types


📌 1. Database Storage
●​ Databases need to be stored physically, and this happens on:​

○​ A server's hard disk.​

●​ A server is:​

○​ A powerful computer that stores data and responds to requests over a network.​

○​ It enables many users to access data at the same time (great for teams and
collaboration).​

🔍 Example
When you make a query, the server processes it and sends back results — all in real time.

📌 2. SQL Data Types


●​ When creating a table, you must define:​

○​ Each field’s name​

○​ Each field’s data type​

●​ Data types tell SQL what kind of information a field holds:​

○​ Numbers (for math)​

○​ Text (for names, addresses, etc.)​

○​ Dates and times​

✅ Why it matters?
●​ Helps SQL know what operations are possible​
e.g. You can add numbers, but not add a name to another name.​
📌 3. Strings (VARCHAR)
●​ A string is a sequence of characters:​

○​ Letters, numbers, spaces, punctuation​

○​ Example: "Maham", "123 ABC St."​

●​ In SQL, the VARCHAR data type is commonly used for strings:​

○​ Flexible: Stores both short and long strings​

○​ Great for fields like: name, title, genre​

📌 4. Integers (INT)
●​ Integers are whole numbers:​

○​ Example: 1001, -25, 2025​

●​ SQL uses the INT data type:​

○​ Can store numbers from −2 billion to +2 billion​

●​ Good for IDs, counts, or anything with no decimals:​

○​ e.g. card_num, age, checkout_id​

📌 5. Floats (NUMERIC)
●​ Floats are numbers with decimal points:​

○​ Example: 2.05, 10.75, 3.14​

●​ In SQL, we often use the NUMERIC type:​

○​ Can store up to 38 digits, including decimals​

●​ Ideal for:​

○​ fines, prices, ratings, etc.​


📌 6. Schemas: Database Blueprints
●​ A schema = A map or blueprint of the database.​

●​ It shows:​

○​ Tables included​

○​ Fields inside each table​

○​ Data types used in each field​

○​ Relationships between tables​

🔍 Example
●​ Schema for a library database might show:​

○​ patrons (card_num INT, name VARCHAR, fines NUMERIC)​

○​ checkouts (checkout_id INT, card_num INT, book_id INT)​

○​ books (book_id INT, title VARCHAR, genre VARCHAR)​

●​ Schema also shows:​

○​ patrons is related to checkouts (through card_num)​

○​ But patrons and books are not directly related​

📌 7. Practice Time!
●​ Now it's time to apply what you’ve learned:​

○​ Choose correct data types​

○​ Interpret schemas​

○​ Understand how data is stored and connected​

✅ Want a Quick-Reference Sheet?


I can create a printable cheat sheet with:

●​ Common SQL data types (incl. BOOLEAN, DATE, etc.)​


●​ Table vs field vs record vs schema​

●​ Real-world analogies for each term​


Just say the word and I’ll whip one up for you!​

CHAPTER 2 - QUERIES

🧠 Introducing SQL Queries: Drawing Insights from Data


📌 1. What Are Queries?
●​ Queries are SQL commands used to ask questions and extract insights from data stored
in databases.​

●​ Example:​

○​ “Which books did James check out in 2022?”​

○​ “Are salaries in Marketing and Accounting departments comparable?”​

📌 2. Why Use SQL?


●​ SQL is ideal for:​

○​ Querying large datasets​

○​ Finding trends in sales, reviews, or user behavior​

○​ Answering specific data questions across multiple tables​

●​ Common organizational uses:​

○​ Website analytics, customer feedback, product performance​

📌 3. SQL Complements Other Tools


●​ SQL is often used alongside Excel, BI tools, etc.​

●​ Great for digging deep into massive datasets that are too big for regular spreadsheets​
📌 4. SQL Keywords: Your Toolbox
🧱 Building Blocks of a Query:
●​ SELECT → Tells SQL which fields (columns) you want​

●​ FROM → Tells SQL which table to get the data from​

💡 Example:
SELECT name

FROM patrons;

This query gets all names from the patrons table.

✅ Conventions:
●​ Capitalize keywords like SELECT, FROM​

●​ Use lowercase for field/table names​

●​ Always end queries with a semicolon (;)​

📌 5. Writing Your First Query


SELECT name

FROM patrons;

●​ This retrieves a result set with the name field from every record in the patrons table.​

📌 6. Selecting Multiple Fields


●​ List fields with commas:​

SELECT card_num, name


FROM patrons;

●​ Add more as needed:​

SELECT name, card_num, total_fine

FROM patrons;

📌 7. Selecting All Fields


●​ Use the wildcard character *:​

SELECT *

FROM patrons;

This grabs all columns from the table — quick and efficient.​

🔹 1. Aliasing
What it means:
Aliasing is renaming a column or table in the result to make it clearer or shorter.

Why use it:


●​ The original column name might be unclear (e.g., name — is it first or last?)​

●​ You want the result to be more readable.​

Example:
SELECT name AS first_name
FROM employees;

Explanation:

●​ name is the actual column in the employees table.​

●​ AS first_name tells SQL: “Show this column in the result as first_name.”​

●​ It does not change the database; just how results look.​

Result:

first_name

Darius

Raven

Eduardo

🔹 2. DISTINCT — Unique Values


What it means:
DISTINCT removes duplicate rows from your result.

Example:
SELECT DISTINCT year_hired
FROM employees;

Explanation:​
If year_hired has repeated values like 2020, 2021, 2020 — SQL will return each year only once.

Result:
year_hired

2020

2017

2022

2021

🔹 3. DISTINCT with Multiple Fields


What it means:
You can apply DISTINCT to multiple columns to get unique combinations.

Example:
SELECT DISTINCT dept_id, year_hired
FROM employees;

Explanation:​
This will only remove rows that are exact duplicates of both fields.

Result:

dept_id year_hired

1 2020

2 2017

2 2022

3 2021

2 2020

🔹 4. Views
What it means:
A view is like a saved query — a virtual table based on a SQL SELECT query.

Why use it:


●​ Reuse complex queries​

●​ Keep results always up to date​

●​ Hide complexity from users​

How to create a view:


CREATE VIEW employee_hire_years AS
SELECT id, name, year_hired
FROM employees;

Explanation:

●​ This saves the above query as a “virtual table” named employee_hire_years.​

●​ You don’t store new data — just the logic of the query.​

How to use the view:


SELECT id, name
FROM employee_hire_years;

Result:​
This behaves just like a regular table:

id name

54378 Darius

94722 Raven

Summary Table
Concept SQL Code Example Purpose

Aliasing SELECT name AS first_name FROM Rename a column in result


employees;

DISTINCT SELECT DISTINCT year_hired FROM Get unique values


employees;

DISTINCT Multiple SELECT DISTINCT dept_id, Get unique combinations of


year_hired FROM employees; values
Create View CREATE VIEW name AS SELECT ... Save a query as a virtual table

Use View SELECT * FROM view_name; Use saved view like a table

Let me know if you want some real-world scenarios or practice questions using these!

Great! Let's break down what you just watched into simple explanations, key differences, and what it
means for you as a learner.

🔹 1. What Are SQL Flavors?


🧠 Simple idea:
SQL flavors are just different "versions" or "dialects" of SQL. They all speak the same language — but
with a slight accent!

Think of them like:

Base Language Accent/Flavor

English British, American...

SQL PostgreSQL, T-SQL...

🔍 Why are there different flavors?


●​ SQL is governed by international standards (ISO and ANSI).​

●​ Companies like Microsoft and Oracle build on top of that standard, adding their own features.​

So they all do basic SQL the same way, but have a few extra tools or different syntax for specific
tasks.

🔹 2. Two Popular SQL Flavors You Should Know


✅ PostgreSQL (aka "Postgres")
●​ Free and open source​

●​ Originally made at University of California, Berkeley​


●​ Used by developers, startups, and companies that prefer open tools​

●​ Supports advanced features (good for analytics)​

✅ SQL Server
●​ Created by Microsoft​

●​ Works best in Windows or Microsoft environments​

●​ The flavor used is called T-SQL (Transact-SQL)​

●​ Often used by enterprise companies, banks, etc.​

🔹 3. Example: Limiting Results — One Task, Two Flavors


You want to show only the first 2 results (useful while testing).

📘 PostgreSQL:
SELECT name, id
FROM employees
LIMIT 2;

🖥️ SQL Server:
SELECT TOP 2 name, id
FROM employees;

✅🔑Both give the same result.​


The only difference is:

●​ PostgreSQL uses LIMIT​

●​ SQL Server uses TOP​

🔹 4. So... Which One Should You Learn First?


🎯 Your Goal Decides:
Situation Go with
Your job/company uses SQL Server Learn T-SQL

You’re freelancing, exploring tech Learn PostgreSQL

You just want to learn SQL Either is fine!

💡 Good news:
●​ 95% of SQL is the same across all flavors.​

●​ Once you know one flavor, switching is like learning the slang of a new city.​

✅ Summary — What You Should Remember:


Concept PostgreSQL SQL Server

Free and open-source? ✅ Yes ✅ Yes (limited version)


Developed by UC Berkeley + DARPA Microsoft

Flavor name PostgreSQL T-SQL

Limit results syntax LIMIT 2 TOP 2

Best for General + analytics Microsoft environments

🚀 Let’s Practice!
Here are 2 quick practice questions:

Q1. Write a query in PostgreSQL to select the first 5 employee names.


SELECT name
FROM employees
LIMIT 5;

Q2. Write the same query in SQL Server:


SELECT TOP 5 name
FROM employees;

Want more practice comparing flavors? Or help installing PostgreSQL to try it out yourself? Just say the
word!
INTERMEDIATE SQL

Here’s a simple recap and explanation of everything you just shared, especially if you're
studying or preparing for a SQL course focused on querying databases:

🔍 Key SQL Concepts Introduced


✅ 1. What is a Query?
A query is simply a request to retrieve data from a database using SQL.

✅ 2. Focus of the Course


●​ The course teaches querying only, not database creation.​

●​ You'll learn how to:​

○​ Filter and sort data​

○​ Use aggregate functions like COUNT()​

○​ Handle common SQL errors​

○​ Follow SQL style and syntax rules​

○​ Use PostgreSQL (a type of database)​

✅ 3. Database in Use: Films


You will practice using a films database with 4 tables:

●​ films​

●​ people​

●​ reviews​

●​ roles​
📊 COUNT() Function in SQL
▶️ COUNT(column_name)
Counts the number of non-null values in a specific column.

Example:

SELECT COUNT(birthdate) FROM people;

→ Returns how many people have a non-missing birthdate

▶️ COUNT(*)
Counts the total number of records (rows) in a table, including ones with missing values.

Example:

SELECT COUNT(*) FROM people;

→ Returns total number of people in the table

▶️ COUNT() for Multiple Fields


You can count more than one column by writing multiple COUNT() functions:

SELECT COUNT(name), COUNT(birthdate) FROM people;

🧹 DISTINCT — Removes Duplicates


SELECT DISTINCT language FROM films;

→ Shows each unique language used in the films.

▶️ COUNT(DISTINCT column_name)
Counts how many unique (non-duplicate) values exist in that column.
SELECT COUNT(DISTINCT birthdate) FROM people;

→ How many different birthdates people have

🎯 Summary Table
Query What It Does

COUNT(*) Total rows in the table

COUNT(column) Non-null values in that column

COUNT(DISTINCT Unique values in that column


column)

DISTINCT Removes duplicate values in the


result

If you want to practice with real examples using the films or people table, I can help you write
or explain any query step by step!

Here’s how you’d write the SQL queries to meet all the instructions using COUNT() and AS for
aliasing:

✅ 1. Count total records in the people table


SELECT COUNT(*) AS count_records
FROM people;

✅ 2. Count how many people have a non-null birthdate


SELECT COUNT(birthdate) AS count_birthdate
FROM people;

✅ 3. Count how many language and country values exist (non-null) in films
SELECT
COUNT(language) AS count_languages,
COUNT(country) AS count_countries
FROM films;
✅ Summary of What You’re Practicing:
Query What it does

COUNT(*) Counts all rows, including those with nulls.

COUNT(colu Counts non-null values only in that column.


mn)

AS Gives your result column a readable name in the


alias_name output.

Let me know if you'd like a breakdown of each result or how DISTINCT can be combined with
these!

Great question! Let's walk through the three versions of SQL code you provided and explain the
difference between DISTINCT, COUNT(), and COUNT(DISTINCT ...), along with the
correct and recommended way to use them.

✅ 1. SELECT DISTINCT country FROM films;


🔹 What this does:
This query returns a list of unique countries from the films table.​
It removes all duplicate country values and shows each country only once.

🧠 Example:
Imagine your films table has the following countries:

USA
UK
USA
France
France

This query will return:

USA
UK
France
❌ 2. SELECT DISTINCT COUNT(country) AS
count_distinct_countries FROM films;
🔹 What this is trying to do:
This code tries to combine DISTINCT with COUNT(column) incorrectly.

❌ Why it’s wrong:


●​ COUNT(country) counts non-null values of country.​

●​ Putting DISTINCT before COUNT() like this doesn't make sense in SQL syntax.​

●​ Most databases (including PostgreSQL) will return an error or a warning.​

✅ 3. SELECT COUNT(DISTINCT country) AS


count_distinct_countries FROM films;
🔹 What this does:
This correctly counts the number of unique countries in the films table.

🧠 Example:
From this data:

USA
UK
USA
France
France
NULL

This query will return:

Because USA, UK, and France are the 3 distinct non-null countries.
📌 Summary Table
Query What it returns Valid
syntax?

SELECT DISTINCT country FROM List of all unique countries ✅


films;

SELECT DISTINCT ❌ Incorrect usage of DISTINCT with


COUNT

COUNT(country) FROM films;

SELECT COUNT(DISTINCT Number of unique country values ✅


country) FROM films;

✅ General Rule to Follow


You want to... Correct Syntax

See all unique values from a column SELECT DISTINCT column_name FROM
table_name;

Count how many unique values SELECT COUNT(DISTINCT column_name)


there are FROM table_name;

Count how many non-null values SELECT COUNT(column_name) FROM


exist table_name;

Count total rows in a table SELECT COUNT(*) FROM table_name;

If you want help writing any of these queries for practice (or on a specific table), just share the
table and your goal. I'll walk you through it step-by-step!

Here's a clear breakdown of what was covered in your video lesson, especially about how SQL
is executed and how to debug common errors:

🧠 1. SQL Query Execution Order (Not Top-to-Bottom)


Even though we write SQL top-down, the database engine processes it in a specific logical
order, roughly like this:

Ste SQL Purpose


p Clause
1️⃣ FROM Tells SQL which table(s) to look at — just like finding the right "closet".

2️⃣ WHERE Filters rows before any selection or grouping.

3️⃣ GROUP BY Groups data for aggregate functions like COUNT(), SUM(), etc.

4️⃣ HAVING Filters groups (not individual rows).

5️⃣ SELECT Picks the columns or calculated fields.

6️⃣ ORDER BY Sorts the results.

7️⃣ LIMIT Limits the number of rows shown.

🔁 Remember: Even though SELECT comes first when you write it, FROM is
processed first internally.

✅ 2. Example: Proper Execution Flow


SELECT name
FROM people
LIMIT 10;

🔍 Here's how it works behind the scenes:


●​ Step 1: Get data from the people table (FROM people)​

●​ Step 2: Pick the name column (SELECT name)​

●​ Step 3: Return only the first 10 rows (LIMIT 10)​

🛠️ 3. Debugging SQL
Common Mistakes & Solutions:
❌ A. Misspelled Field Names
SELECT nme FROM people;
-- Error: column "nme" does not exist
✅ Fix:
SELECT name FROM people;

❌ B. Missing Commas Between Fields


SELECT title country duration FROM films;
-- Caret (^) might point to "country", but the issue is missing comma

✅ Fix:
SELECT title, country, duration FROM films;

❌ C. Misspelled Keywords
SELEC name FROM people;
-- Error: syntax error at or near "SELEC"

✅ Fix:
SELECT name FROM people;

💡 Final Tips
●​ Pay attention to the caret ( ^ ) in SQL error messages — it shows where the error is or
starts.​

●​ Commas are needed between field names in SELECT.​

●​ Keywords like SELECT, FROM, WHERE must be correctly spelled.​

●​ SQL is case-insensitive, but tables and field names may not be, especially in some
environments (e.g., PostgreSQL if double quotes are used).​

📌 Quick Debug Checklist


Issue What to Check

Column not found Check spelling and table name

Syntax error Look at commas, parentheses, and keyword


spelling
Unexpected result Double-check WHERE, DISTINCT, or JOIN logic

Alias not recognized Make sure alias is declared before it's referenced

Let me know if you'd like to practice with some error debugging questions or need help fixing a
real query!

Great! Here's a clear breakdown and explanation of the SQL style and formatting concepts
from your video lesson — with examples, best practices, and why it all matters:

🧠 1. SQL Style = Writing Neatly


Even though SQL doesn’t care about formatting, humans do. Writing readable SQL helps
others (and your future self) understand and debug queries easily.

✍️ 2. Messy vs. Clean SQL


❌ Poorly formatted SQL (works, but hard to read)
SELECT title,release_year,country FROM films LIMIT 3;

✅ Well-formatted SQL (clean and readable)


SELECT
title,
release_year,
country
FROM
films
LIMIT 3;

💡 Tip: Capitalize keywords (SELECT, FROM, WHERE, etc.) and use new lines for
clauses and fields.

✅ 3. Best Practices Summary


Practice Example Why It Helps

Capitalize SQL keywords SELECT, FROM, WHERE Easy to spot logic blocks
New line per clause SELECT ... \n FROM Visual separation of logic
... \n WHERE ...

Indent fields Each field in new line Cleaner with long SELECT lists

Use semicolons ... LIMIT 10; Ends query clearly, required in


some SQL types

Use double quotes for "release year" Handles field names with spaces
odd field names or special characters

🔍 4. Semicolon Usage
●​ PostgreSQL: Semicolon (;) is optional in a single query.​

●​ Other SQL dialects (e.g., MySQL, Oracle): Often required.​

●​ Best Practice: Always include ; to avoid future errors and support multi-query scripts.​

🔧 5. Weird Field Names? Use Quotes


If someone named a column with spaces (not recommended), you must use double quotes:

SELECT
title,
"release year",
country
FROM
films;

❌ Avoid naming fields like release year.​


✅ Better: release_year (with underscore)

📘 6. SQL Style Guides


Examples include:

●​ SQL Style Guide by Simon Holywell (commonly used)​


●​ Your organization might have its own standard.​

Main ideas:

●​ Consistent indentation​

●​ Naming conventions (snake_case, CamelCase, etc.)​

●​ Logical ordering of statements​

🤝 7. Why Formatting Matters


Benefit Example

Easier debugging Quickly spot where things go wrong

Better collaboration Teammates can read and reuse your code

Professionalism Clean SQL = Good impression in work and interviews

Maintainability Easy to return and edit weeks/months later

🚀 8. Practice Example (Proper Formatting)


Task: Get all films from 2005, show title, duration, release_year.
✅ Good SQL:
SELECT
title,
duration,
release_year
FROM
films
WHERE
release_year = 2005;

If you'd like, I can give you some messy queries to clean up or test you with formatting drills.
Just say the word!

-​ Use “” around non standard names - sometimes


Absolutely! Let's break this down into simple ideas using everyday examples so you get a
clear understanding of the WHERE clause and how comparison operators work in SQL.

🧠 1. What is WHERE in SQL?


Think of a database like a closet full of coats. If you want a green coat, you don't take all of
them—you filter and only pick the green one.

👉
In SQL, the WHERE clause is your filter tool. You use it to say:​
“I only want rows where something is true.”

✅ 2. Syntax Template:
SELECT column_name
FROM table_name
WHERE condition;

💡 3. Examples with Numbers


a. Greater than >
SELECT *
FROM films
WHERE release_year > 1960;

→ Returns all films released after 1960

b. Less than <


SELECT *
FROM films
WHERE release_year < 1960;

→ Returns all films before 1960

c. Less than or equal to <=


SELECT *
FROM films
WHERE release_year <= 1960;

→ Returns films before or in 1960

d. Equal to =
SELECT *
FROM films
WHERE release_year = 1960;

→ Returns only films from 1960

e. Not equal to != or <>


SELECT *
FROM films
WHERE release_year <> 1960;

→ Returns all films except those from 1960

💬 4. Examples with Text (Strings)


When you're filtering text values, you need single quotes.

SELECT *
FROM films
WHERE country = 'Japan';

→ Only returns films where the country is Japan

🧩 5. Order of Written SQL vs Order of Execution


Even though you write SQL like this:

SELECT title
FROM films
WHERE country = 'Japan'
LIMIT 5;

The computer actually runs it in this order:

1.​ FROM → Go to the table​

2.​ WHERE → Filter the rows (e.g., country = 'Japan')​

3.​ SELECT → Pick the columns (title)​

4.​ LIMIT → Keep only the top 5 results​

🔄 Summary of Comparison Operators


Operator Meaning Example

> Greater than / After release_year >


2000

< Less than / Before release_year <


1990

>= Greater than or rating >= 8


equal

<= Less than or equal rating <= 7

= Equal to country = 'USA'

<> or != Not equal to release_year <>


2020

👀 Final Tip
To avoid errors, always:

●​ Use 'single quotes' for text​

●​ Write clean and readable SQL using indentation and uppercase keywords​
Would you like a small quiz or exercise to test this?

Great! You're asking how to write and understand SQL queries that use WHERE with numbers.
Let's go through each task, explain why the correct query works, and help you understand how
to generalize this for any similar case in the future.

✅ Task 1:
"Select the film_id and imdb_score from the reviews table and filter on scores higher
than 7.0."

✅ Correct SQL:
SELECT film_id, imdb_score
FROM reviews
WHERE imdb_score > 7.0;

✅ Why this works:


●​ SELECT: Pick only these two columns.​

●​ FROM reviews: Use the reviews table.​

●​ WHERE imdb_score > 7.0: Only show rows with scores greater than 7.​

🧠 General Rule for Task 1:


When filtering numeric values:

SELECT column1, column2


FROM table_name
WHERE numeric_column [>,<,=,>=,<=] value;

✅ Task 2:
"Select the film_id and facebook_likes of the first ten records with less than 1000
likes from the reviews table."

✅ Correct SQL:
SELECT film_id, facebook_likes
FROM reviews
WHERE facebook_likes < 1000
LIMIT 10;

✅ Explanation:
●​ You’re selecting only rows where facebook_likes are less than 1000.​

●​ LIMIT 10 gives the first 10 results from that filtered group.​

🧠 General Rule for Task 2:


When you need limited results from a filtered dataset:

SELECT column1, column2


FROM table_name
WHERE condition
LIMIT number_of_rows;

✅ Task 3:
"Count how many records have a num_votes of at least 100,000; use the alias
films_over_100K_votes."

✅ Correct SQL:
SELECT COUNT(*) AS films_over_100K_votes
FROM reviews
WHERE num_votes >= 100000;

✅ Explanation:
●​ COUNT(*) counts how many records meet the condition.​

●​ WHERE num_votes >= 100000 ensures only votes >= 100K are counted.​

●​ AS films_over_100K_votes gives your result a readable name.​


🧠 General Rule for Task 3:
When counting rows that meet a numeric condition:

SELECT COUNT(*) AS alias_name


FROM table_name
WHERE numeric_column >= some_value;

🔁 Summary Table
Task SQL Concept Keyword Example

Filter by number WHERE imdb_score >


7.0

Limit rows LIMIT LIMIT 10

Count rows COUNT(*) + num_votes >=


WHERE 100000

🎯 Tips to Generalize:
1.​ Start with the table and columns you want.​

2.​ Add a WHERE clause if you want to filter.​

3.​ Use COUNT(*) to count rows, and add AS to give your result a label.​

4.​ Use LIMIT to get a specific number of rows.​

5.​ Use these comparison operators: =, !=, >, <, >=, <=.​

Want practice questions to help you master this?

Great question — let’s clear up your confusion about the COUNT() function, especially why
sometimes we use COUNT(*) and sometimes COUNT(column_name).

🔍 What does COUNT() do?


The COUNT() function counts how many rows match a condition.

✅ Difference between COUNT(*) and


COUNT(column_name)
Function What it does When to use

COUNT(*) Counts all rows, even if Use when you just want to know how
there are NULLs many rows total meet the condition

COUNT(column_n Counts only non-null Use when you're only interested in


ame) values in that specific counting values present in a specific
column field

✅ Your Example:
SELECT COUNT(*) AS films_over_100K_votes
FROM reviews
WHERE num_votes >= 100000;

❓ Why COUNT(*) here?


●​ You are asking: "How many records (rows) have num_votes ≥ 100,000?"​

●​ Since the WHERE condition already ensures num_votes ≥ 100,000, we just want to
count how many rows passed that filter — not the actual number of num_votes
values.​

➡️ So COUNT(*) is correct here because:


●​ You're counting rows after filtering, not counting how many non-null values are in a
specific column.​

🔁 If You Wrote:
SELECT COUNT(num_votes)
FROM reviews
WHERE num_votes >= 100000;
This would still work, because:

●​ The WHERE clause already filtered out NULLs and low vote values.​

●​ But it’s more standard and clearer to use COUNT(*) when you're simply counting
rows, not specific values.​

💡 When to Use Which:


Use COUNT(*):
●​ When you're asking: "How many rows match this condition?"​

Example:

SELECT COUNT(*) FROM reviews WHERE imdb_score > 8;

Use COUNT(column_name):
●​ When you're asking: "How many rows have a value in this column?"​

Example:

SELECT COUNT(birthdate) FROM people;


-- Counts only people who have a non-null birthdate.

✅ Key Takeaway
🔸 Use COUNT(*) to count all rows that meet your condition (including rows where

🔸 Use COUNT(column) when you're counting how many non-null entries exist in
some columns might be NULL).​

that column.

Let me know if you'd like a few mini practice questions to test this!
WHERE with text:

-- Count the Spanish-language films


SELECT COUNT(* or language) AS count_spanish
FROM films
WHERE language = 'Spanish';

Perfect! You’re now learning how to filter your SQL queries using multiple criteria — this is
essential for building real-world database queries. Let me break down what you just learned into
a clear, simple explanation with examples, and show you how to generalize it for practice and
mastery.

🧠 3 Main Keywords for Multiple Criteria in SQL


These are used with the WHERE clause to make your filters more precise:

1. OR ➝ One condition must be true


SELECT title
FROM films
WHERE release_year = 1994 OR release_year = 2000;

✅ This returns films released in 1994 OR 2000.


2. AND ➝ All conditions must be true
SELECT title
FROM films
WHERE release_year >= 1994 AND release_year <= 2000;

✅ This returns films released between 1994 and 2000, including both years.
3. BETWEEN ➝ Shorthand for a range (inclusive of start and end)
SELECT title
FROM films
WHERE release_year BETWEEN 1994 AND 2000;

✅ Same as the AND example above, just shorter and more readable.

⚠️ Important Rules
✅ Always repeat the field name with OR and AND
❌ Wrong:
WHERE release_year = 1994 OR 2000

✅ Right:
WHERE release_year = 1994 OR release_year = 2000

🔁 Combining AND + OR ➝ Use Parentheses!


When combining them, use parentheses to control the logic. Without parentheses, SQL can
misinterpret your logic.

SELECT title
FROM films
WHERE (release_year = 1994 OR release_year = 1995)
AND (certification = 'PG' OR certification = 'R');

✅ This means:
●​ The film is from 1994 or 1995​

●​ AND it is certified PG or R​

✨ Example: Mix BETWEEN, AND, and OR


SELECT title
FROM films
WHERE release_year BETWEEN 1994 AND 2000
AND country = 'United Kingdom';

✅ This gives you UK films from 1994 to 2000 (inclusive).

✅ General Rules to Learn and Practice


Goal SQL Example Notes

Filter for one of several values OR Repeat the field


name

Filter for two or more conditions that must all be AND Repeat field names
true

Filter for a value within a range BETWEEN x AND Inclusive


y

Combine filters correctly Use parentheses Prevent logic errors


()

🧪 Practice Tips:
Try writing SQL queries for:

1.​ Films from USA or UK​

2.​ Films with imdb_score > 7 and num_votes > 100000​

3.​ Films released between 2010 and 2015 with imdb_score > 8​

4.​ Films from India or Pakistan and facebook_likes < 500​

-- Select all records for German-language films released after 2000 and
before 2010
SELECT *
FROM films
WHERE release_year > 2000
​ AND release_year < 2010
​ AND language = 'German';
Using OR
This time you'll write a query to get the title and release_year of films released in 1990 or
1999, which were in English or Spanish and took in more than $2,000,000 gross.

SELECT title, release_year


FROM films
WHERE (release_year = 1990 OR release_year = 1999)
AND (language = 'English' OR language = 'Spanish')
-- Filter films with more than $2,000,000 gross
AND (gross > 2000000);

Let's use BETWEEN with AND on the films database to get the title and release_year of all
Spanish-language films released between 1990 and 2000 (inclusive) with budgets over $100
million.

SELECT title, release_year


FROM films
WHERE release_year BETWEEN 1990 AND 2000
AND budget > 100000000
-- Amend the query to include Spanish or French-language films
AND (language = 'Spanish' OR language= 'French');

FILTERING TEXT

Absolutely! You’re now diving into the really fun part of SQL — filtering text and
working with patterns using LIKE, IN, and NOT LIKE. Let's make this super
clear and organized, so you feel confident and can use these tools like a pro.

🧠 3 Key Concepts for Filtering Text in SQL


🔹 1. LIKE — Matching patterns (case-sensitive!)
Used with WHERE to find partial matches using wildcards.
Wildcar Meaning Example
d

% zero, one, or many 'A%' matches Adel,


characters Alice, A

_ exactly one character 'A__' matches Amy,


Ana, Ali

✅ Syntax:
SELECT title
FROM films
WHERE title LIKE 'Star%';

→ Finds films with titles starting with “Star”.

🔹 2. NOT LIKE — Exclude patterns


Works the same way as LIKE but excludes matching values.

✅ Syntax:
SELECT name
FROM people
WHERE name NOT LIKE 'A%';

→ Finds all names not starting with A.

🔹 3. IN — Match exact values from a list


Use when you want to match from multiple values (like several countries or
years).

✅ Syntax:
SELECT title
FROM films
WHERE country IN ('Germany', 'France');

→ Matches titles with country = Germany or France.

✅ How to Master the Code (Generalized Guide)


🎯 1. Use LIKE with % for flexible matching:
-- Starts with 'A'
WHERE name LIKE 'A%'

-- Ends with 'y'


WHERE name LIKE '%y'

-- Contains 'ell'
WHERE name LIKE '%ell%'

-- Second letter is 'e'


WHERE name LIKE '_e%'

🎯 2. Use IN for clean multiple matches:


Instead of this:

WHERE country = 'USA' OR country = 'UK' OR country = 'France'

Write this:

WHERE country IN ('USA', 'UK', 'France')

🎯 3. Use NOT LIKE to exclude unwanted patterns:


-- Exclude names that start with Z
WHERE name NOT LIKE 'Z%'

-- Exclude names ending in "son"


WHERE name NOT LIKE '%son'
💡 Tips for Practicing (With Examples)
Practice Goal Query Example
Find all films that start with WHERE title LIKE 'The%'
“The”
Find all names that are 3 letters WHERE name LIKE '___'

Exclude films with "War" in title WHERE title NOT LIKE


'%War%'

Match release years 1920, WHERE release_year IN


1930, 1940 (1920, 1930, 1940)

Find people whose name ends WHERE name LIKE '%n'


with ‘n’

🧪 Challenge Yourself!
Try writing SQL for:

1.​ Titles that contain the word "Love"​

2.​ Names that are 4 characters long​

3.​ Countries not named India or China​

4.​ Films where the third letter is “a”​

5.​ Titles starting with either "The" or "A"​

Would you like me to create a worksheet, a quiz, or even a mini project for you
to practice these skills further?
SELECT name

FROM people

-- Select the names that have r as the second letter

WHERE name LIKE '_r%';

So what does '_r%' mean?

It matches any name where:

1.​ The first character can be anything → _​

2.​ The second character is exactly r → r​

3.​ The rest of the name can be anything, including nothing → %

-- Find the title, certification, and language all films


certified NC-17 or R that are in English, Italian, or Greek

SELECT title, certification, language

FROM films

WHERE certification IN ('NC-17', 'R')

​ AND language IN ('English', 'Italian', 'Greek');

Let's break down the solution to understand how it addresses the exercise requirements:

1.​ SELECT Clause:


○​ SELECT title, certification, language: This part of the query specifies that we
want to retrieve the title, certification, and language columns from the films
table. These are the specific pieces of information the exercise asks us to extract.
2.​ FROM Clause:
○​ FROM films: This indicates that the data we are querying comes from the films
table. This is the source of our data.
3.​ WHERE Clause:
○​ WHERE certification IN ('NC-17', 'R'): This condition filters the results to
include only those films whose certification is either 'NC-17' or 'R'. The IN
operator is used here to check if the certification value matches any of the values
in the specified list ('NC-17', 'R').
○​ AND language IN ('English', 'Italian', 'Greek'): This additional condition
further filters the results to include only films where the language is either 'English',
'Italian', or 'Greek'. Again, the IN operator is used to match the language against the
specified list of languages.

By combining these conditions with the AND operator, the query ensures that only films meeting both
criteria (certification and language) are selected. This solution effectively retrieves the desired
information as per the exercise instructions.

-- Count the unique titles


SELECT COUNT(DISTINCT title) AS nineties_english_films_for_teens
FROM films
-- Filter to release_years to between 1990 and 1999
WHERE release_year BETWEEN 1990 AND 1999
-- Filter to English-language films
​ AND language = 'English'
-- Narrow it down to G, PG, and PG-13 certifications
​ AND certification IN ('G', 'PG', 'PG-13');

Sure! Let’s break this SQL code down and give you a clear guide on when to
use brackets (parentheses), what each part means, and when they’re
necessary or optional.

🔍 THE SQL QUERY:


-- Count the unique titles
SELECT COUNT(DISTINCT title) AS nineties_english_films_for_teens
FROM films
-- Filter to release_years to between 1990 and 1999
WHERE release_year BETWEEN 1990 AND 1999
-- Filter to English-language films
AND language = 'English'
-- Narrow it down to G, PG, and PG-13 certifications
AND certification IN ('G', 'PG', 'PG-13');

📌 WHEN TO USE BRACKETS (PARENTHESES) IN SQL


Brackets or parentheses () are used only when needed for clarity or to
define precedence, especially when:

✅ 1. Functions need them


They’re required for functions like:

●​ COUNT()​

●​ SUM()​

●​ AVG()​

●​ DISTINCT inside a function also needs to be in parentheses.​

📌 Example:
COUNT(DISTINCT title)

This counts unique titles — so the brackets group DISTINCT title together
and pass that to the COUNT() function.

✅ 2. The IN operator requires them


Use parentheses with IN to list multiple values to match.

📌 Example:
certification IN ('G', 'PG', 'PG-13')

You must wrap the list in () — it's like saying: "Is certification in this set?"

✅ 3. To control logic precedence (AND/OR)


If you mix AND and OR, parentheses are strongly recommended to make your
logic clear.
📌 Example:
WHERE (language = 'English' OR language = 'French')
AND release_year > 2000

Without parentheses, SQL might misinterpret the order.

🚫 WHEN YOU DO NOT NEED BRACKETS


❌ 1. For BETWEEN ... AND ...
No need for parentheses here — BETWEEN handles the range cleanly.

📌 Example:
release_year BETWEEN 1990 AND 1999

This is fine as is.

❌ 2. Basic comparisons
Comparisons like language = 'English' or release_year > 2000 don’t
need brackets.

✍️ EXPLANATION OF THE GIVEN QUERY:


SELECT COUNT(DISTINCT title) AS nineties_english_films_for_teens

●​ COUNT(DISTINCT title) → counts unique film titles.​

●​ AS nineties_english_films_for_teens → gives a custom name to


the result column.​

FROM films
●​ We're querying from the films table.​

WHERE release_year BETWEEN 1990 AND 1999

●​ Only films released from 1990 to 1999 (inclusive).​

AND language = 'English'

●​ Filters only English-language films.​

AND certification IN ('G', 'PG', 'PG-13');

●​ Filters certifications to kid-friendly ones.​

✅ QUICK GUIDE TO BRACKETS:


Use Case Use Brackets? Example
SQL function like
COUNT
✅ Yes COUNT(DISTINCT
title)

List values in IN ✅ Yes IN ('G', 'PG')

Combining AND/OR ✅
Recommended
(a = 1 OR b = 2)
conditions AND c = 3

BETWEEN condition ❌ No BETWEEN 1990 AND


1999

Simple comparisons ❌ No language =


'English'
If you want, I can give you some practice questions to test your bracket-usage
intuition.

Great! Here's a clear and practical summary of how to work with NULL values
in SQL, so you can understand and remember it well:

🔹 What is NULL in SQL?


●​ NULL means a missing, unknown, or unrecorded value.​

●​ It's not zero, not empty string, and not “N/A”—it's a special SQL marker
for "no value".​

🔹 Why does NULL matter?


Missing data can lead to:

●​ Wrong results (e.g. assuming every person in your table has a birthdate or
deathdate).​

●​ Incomplete analysis if not handled properly.​

🔹 Key SQL Syntax for NULL:


✅ To find missing values:
SELECT *
FROM table_name
WHERE column_name IS NULL;

Example: Who doesn’t have a birthdate?

SELECT name
FROM people
WHERE birthdate IS NULL;
✅ To exclude missing values:
SELECT *
FROM table_name
WHERE column_name IS NOT NULL;

Example: Who does have a birthdate?

SELECT name
FROM people
WHERE birthdate IS NOT NULL;

🔹 COUNT with NULL values


✅ To count all records (including ones with NULLs in specific columns):
SELECT COUNT(*) FROM people;

✅ To count only records where a column is not NULL:


SELECT COUNT(birthdate) FROM people;

Or equivalently:

SELECT COUNT(*) FROM people WHERE birthdate IS NOT NULL;

Both of the above will return the same result, because COUNT(column_name)
ignores NULLs automatically.

🔹 Easy rule to remember


You want to... Use this
See what’s missing WHERE column IS NULL

Filter out what's missing WHERE column IS NOT NULL

Count all rows COUNT(*)


Count only non-NULL in a COUNT(column_name) or WHERE
column column IS NOT NULL

🧠 Real-world tip:
Always check for NULLs before analyzing a dataset. It saves you from
incorrect conclusions.

Would you like me to give you 2–3 practice questions with answers based on
this?

Absolutely! Let's walk through your SQL code step-by-step, explain it in plain
language, and teach you how to generalize and use these patterns for other
queries.

✅ 1. List all film titles with missing budget


SELECT title AS no_budget_info
FROM films
WHERE budget IS NULL;

🔍 What this does:


●​ SELECT title: You're asking for the names of films.​

●​ AS no_budget_info: This renames the title column in the result to


no_budget_info (this is optional, but useful for clarity).​

●​ FROM films: You're pulling data from the table called films.​

●​ WHERE budget IS NULL: You only want to see rows where the budget
is missing (i.e., the budget column has a NULL value).​

✅ Real-world meaning:
You want a list of all films in the films table where the budget is not
recorded. These are entries where the budget field is blank, unknown,
or never entered.


for
2. Count the number of films we have language data

SELECT COUNT(*) AS count_language_known


FROM films
WHERE language IS NOT NULL;

🔍 What this does:


●​ SELECT COUNT(*): This counts the number of rows that match your filter.​

●​ AS count_language_known: This renames the output column to


something meaningful (count_language_known).​

●​ FROM films: You're still working in the films table.​

●​ WHERE language IS NOT NULL: This filters out any rows where the
language is missing.​

✅ Real-world meaning:
You're counting how many films do have language data available (not
missing). You are filtering out entries where the language field is
NULL.

🧠 Summary: When to use IS NULL vs IS NOT NULL


Goal Syntax Example use case
Find rows with missing WHERE column Films with no budget
values IS NULL
Find rows with available WHERE column Films that have language
(non-missing) values IS NOT NULL info

Count non-missing records SELECT Count of release years


in a column COUNT(column) that are known

Count rows with a filter SELECT Count all rows where


COUNT(*) WHERE rating is not missing
...

💡 How to use this in your own work:


When you get a dataset (table), and you want to:

●​ Investigate missing info → Use IS NULL.​

●​ Only work with complete data → Use IS NOT NULL.​

●​ Count or list records depending on whether they have certain data or


not → Use COUNT() with or without filtering.​

✍️ Practice Idea:
Try writing these queries:

1.​ Count how many films do not have a known release year.​

2.​ List all film titles where the director name is missing.​

3.​ Count how many films do have a runtime.​

Let me know if you want me to walk through any of these!


CHAPTER 3 AGGREGATE FUNCTIONS

Awesome! You're now learning a super useful part of SQL: aggregate


functions — which help summarize your data.
Let me break down what you just learned in a simple way and show
you how to use each function with example queries you can practice
right away.

🔢 1. Aggregate Functions Overview


Functi Purpose Works On Works On
on Numbers? Text?
COUNT Counts values (non-NULL
by default)
✅ ✅
()

AVG() Calculates average ✅ ❌


SUM() Adds up values ✅ ❌
MIN() Finds the smallest value ✅ ✅
MAX() Finds the largest value ✅ ✅
📘 2. Examples for Each Function
✅ COUNT() — Count non-missing records
SELECT COUNT(language) AS count_known_languages
FROM films;

✅ AVG() — Average of numeric values


SELECT AVG(budget) AS average_film_budget
FROM films;
✅ SUM() — Total of numeric values
SELECT SUM(budget) AS total_budget_all_films
FROM films;

✅ MIN() — Minimum value


SELECT MIN(budget) AS smallest_budget
FROM films;

✅ MAX() — Maximum value


SELECT MAX(country) AS alphabetically_last_country
FROM films;

💡 3. Aliasing
You saw this line:
SELECT MAX(country) AS max_country

This is called aliasing (AS) — it renames the column in the output so


it's easier to understand. Without aliasing, you'd see something like
max(country) in the result header, which looks messy and less
readable.

🧠 4. Using Aggregate Functions with Non-Numeric Data


●​ MIN(country) ➝ First alphabetically​

●​ MAX(release_date) ➝ Most recent release​

●​ COUNT(name) ➝ Number of non-NULL names​


🚫 5. What You Can't Do
You can’t do:
SELECT SUM(name) --
text
❌ This will give an error because name is

🏁 Summary Cheat Sheet


SELECT COUNT(field_name) AS alias -- Counts non-NULL values
SELECT AVG(numeric_field) AS alias -- Gets the average
SELECT SUM(numeric_field) AS alias -- Adds all values
SELECT MIN(field_name) AS alias -- Smallest (alphabetically,
numerically, or date-wise)
SELECT MAX(field_name) AS alias -- Largest (alphabetically,
numerically, or date-wise)

Let me know if you want practice exercises or a small quiz to test


yourself! 3 EXAMPLES:
-- Calculate the highest gross film released between 2000-2012
SELECT MAX(gross) AS highest_gross
FROM films
WHERE release_year BETWEEN 2000 AND 2012;
-- Calculate the sum of gross from the year 2000 or later
SELECT SUM(gross) AS total_gross
FROM films
WHERE release_year >= 2000;
-- Calculate the average gross of films that start with A
SELECT AVG(gross) AS avg_gross_A
FROM films
WHERE title LIKE 'A%';
Great job keeping up! You’re now learning to combine filtering and
summarizing, plus how to clean up numbers using ROUND(). Let me
break everything down clearly with code examples, explanations, and
how each part works step-by-step.

🔁 1. Combining WHERE with Aggregate Functions


⛏️ Why?
Because WHERE filters the rows first, and then aggregate functions
like AVG, SUM, MIN, MAX, and COUNT apply to the filtered data only.

🧪 Example 1: Average budget for movies after 2010


SELECT AVG(budget) AS avg_budget
FROM films
WHERE release_year >= 2010;

📌 How it works:
●​ First filters rows with release_year >= 2010​

●​ Then calculates the average of the budget column only from


those rows​

🧪 Example 2: Total budget in 2010


SELECT SUM(budget) AS total_2010_budget
FROM films
WHERE release_year = 2010;
🧪 Example 3: Smallest budget in 2010
SELECT MIN(budget) AS smallest_2010_budget
FROM films
WHERE release_year = 2010;

🧪 Example 4: Highest budget in 2010


SELECT MAX(budget) AS biggest_2010_budget
FROM films
WHERE release_year = 2010;

🧪 Example 5: Count of budgets recorded in 2010 (non-missing values)


SELECT COUNT(budget) AS num_budgets_2010
FROM films
WHERE release_year = 2010;

Note: COUNT(budget) ignores NULL values, so this counts only valid


budget entries.

🔄 2. Using ROUND() to Clean Up Numbers


📉 Example 1: Round to 2 decimal places (useful for currency)
SELECT ROUND(AVG(budget), 2) AS avg_budget_rounded
FROM films
WHERE release_year >= 2010;

🎯 This takes the average budget and rounds it to 2 decimal places.


🔢 Example 2: Round to a whole number
SELECT ROUND(AVG(budget)) AS avg_budget_whole
FROM films
WHERE release_year >= 2010;

This is the same as using ROUND(AVG(budget), 0).

🧠 Example 3: Round to the left of the decimal point


SELECT ROUND(AVG(budget), -5) AS rounded_to_nearest_100k
FROM films
WHERE release_year >= 2010;

●​ -5 rounds to the left (e.g., 123456789 becomes 123500000)​

●​ Handy for simplifying very large values (e.g., total budgets)​

✅ Summary
Function Purpose
WHERE Filters rows before
summarizing
AVG(budg Averages the filtered rows'
et) budget

ROUND(.. Rounds the result to 2


., 2) decimal places
ROUND(.. Rounds to a whole number
., 0) (default)

ROUND(.. Rounds to the left of decimal


., -5) (e.g., 100k)

Let me know if you'd like a mini-quiz, or want to try writing your own
queries for practice!
Here’s a complete set of study notes for the topic: Aliasing and
Arithmetic in SQL, covering concepts, explanations, and examples.

🧾 SQL Notes: Arithmetic, Aliasing, and Execution Order


🔢 1. Arithmetic in SQL
✅ SQL supports basic math using symbols:
Operation Sym Example Res
bol ult
Add + SELECT 2 5
+ 3;
Subtrac - SELECT 6
t 10 - 4;
Multiply * SELECT 6 18
* 3;
Divide / SELECT 8 4
/ 2;

✅ You can also do math with columns:


SELECT gross - budget
FROM films;

🎯 This calculates profit for each movie.


⚠️ 2. Watch Out: Integer Division in SQL
💡 SQL follows integer division rules:
SELECT 4 / 3; -- Returns: 1 (no decimals!)
SELECT 4.0 / 3.0; -- Returns: 1.333...

🧠 Tip: Add decimal points (.0) for precise decimal results.


🔁 3. Aggregate Functions vs. Arithmetic
Concept Works Across Example
Arithmetic Row by row gross - budget = profit
(horizontal)
Aggregate Column-wise AVG(budget) = overall
Functions (vertical) average budget

🏷️ 4. Aliasing with AS
🔍 Why Use Aliases?
1.​Makes the result easier to understand​

2.​Prevents confusing default names (like just max)​

3.​Essential when using math or functions​

🧪 Example:
SELECT gross - budget AS profit
FROM films;

✅ AS profit gives the calculated column a meaningful name.


🧠 5. Aliasing with Functions
SELECT MAX(budget) AS highest_budget,
MAX(gross) AS highest_gross
FROM films;

📌 Without aliasing, both columns would just be named max—which is


confusing!

📐 6. SQL Order of Execution (Important for Aliases)


St Clau Description
ep se
1 FRO Which table we're working with
M
2 WHE Filters rows
RE
3 SEL Chooses columns, performs
ECT math/aggregates

4 LIM Limits the number of output


IT rows

🚫 You cannot use an alias inside WHERE because aliases are not
yet created at that point.
🧪 Incorrect Example (will cause error):
SELECT gross - budget AS profit
FROM films
WHERE profit > 10000000; -- ❌
Error: "profit" alias not available yet

✅ Correct Solution: Use full expression in WHERE


SELECT gross - budget AS profit
FROM films
WHERE gross - budget > 10000000;

✅ Summary Table
Concept Key Point
Arithmetic Use +, -, *, / for math in SQL
Integer Returns whole numbers unless you
Division use decimals (.0)
Row Arithmetic Row-wise calculations like gross -
budget
Aggregate Column-wise summaries like
Functions SUM(budget)
Aliases with Rename result columns for clarity (AS
AS profit)
Execution Alias is not available in WHERE, only
Order after SELECT

Let me know if you want a practice worksheet, a mini-quiz, or a


cheat sheet PDF for this topic!
SORTING AND GROUPING

Here’s a clear and detailed set of notes to help you master sorting
results in SQL using ORDER BY, ASC, DESC, and multiple sorting levels.
This is essential for organizing your output and making data easier to
interpret.

🧾 SQL Notes: Sorting Results with ORDER BY


📘 1. What is Sorting?
Sorting helps organize data in a sequence so it’s easier to read, compare,
and analyze.

Just like sorting clothes by size or color makes it easier to choose, sorting
in SQL lets us:

●​ Quickly find the highest or lowest values​

●​ Alphabetize text​

●​ Apply priority logic (e.g. sort by rating, then year)​

🔹 2. ORDER BY Basics
✅ Syntax:
SELECT column1, column2
FROM table_name
ORDER BY column_name;
●​ Default sort is ascending (lowest → highest or A → Z).​

●​ You don’t need to sort only the fields you’re selecting — but it's better
for clarity.​

🧪 Example 1: Sort by numeric column


SELECT title, budget
FROM films
ORDER BY budget;

→ Sorts from smallest to largest budget (ascending by default).

🧪 Example 2: Sort alphabetically


SELECT title
FROM films
ORDER BY title;

→ Sorts titles A → Z (symbols and numbers appear first).

🔸 3. Use ASC and DESC for Clarity


🔼 Ascending (Low → High or A → Z)
ORDER BY release_year ASC;

🔽 Descending (High → Low or Z → A)


ORDER BY imdb_score DESC;

🔸 4. Filtering NULLs Before Sorting


Sorting on fields with NULL can make the output messy. Filter them out:
SELECT title, budget
FROM films
WHERE budget IS NOT NULL
ORDER BY budget DESC;

🔸 5. Sorting Without Selecting That Field


This works:

SELECT title
FROM films
ORDER BY release_year;

But this is better:

SELECT title, release_year


FROM films
ORDER BY release_year;

✅ Including the sorted column in your results makes it easier to


understand the order.

🔸 6. Sort by Multiple Fields (Tie-breaker logic)


SELECT title, oscars_won, imdb_score
FROM films
ORDER BY oscars_won DESC, imdb_score DESC;

✅ First, it sorts by oscars_won.​


If there’s a tie, it then sorts those tied rows by imdb_score.

🔸 7. Different Order Directions


SELECT name, birthdate
FROM people
ORDER BY birthdate ASC, name DESC;

→ Sorts birthdate from oldest to youngest, and among those with the
same birthday, shows names from Z → A.

🔁 8. SQL Order of Execution (Updated)


Ste Claus Description
p e
1 FROM Pull table

2 WHERE Filter rows


3 SELEC Choose columns &
T compute values

4 ORDER Sort rows


BY
5 LIMIT Trim to top X rows

🚫 You cannot use an alias from SELECT inside WHERE or ORDER BY


unless you're using a subquery.

✅ Quick Summary Table


Keywo Description
rd

ORDER Sorts data based on


BY column(s)

ASC Sorts in ascending order


(default)

DESC Sorts in descending order


, Add multiple fields to sort by

NULL Filter out if not meaningful for


sorting

-- Select the title and duration from longest to shortest film

SELECT title, duration

FROM films

ORDER BY title, duration DESC;

Here’s a clean and clear study guide on how to use the GROUP BY clause
in SQL — one of the most powerful tools when you want summarized
insights from different categories or groups in your data.

🧾 SQL Notes: GROUPING Data with GROUP BY


📘 1. Why Use GROUP BY?
GROUP BY is used when you want to:

●​ Summarize data by categories​

●​ Apply aggregate functions (like COUNT(), AVG(), SUM(), etc.)​

●​ See summary statistics per group instead of per row​

Example:

“What’s the average movie duration for each film certification?”​


→ You'd group by certification and calculate the
AVG(duration).
🔹 2. Syntax of GROUP BY
SELECT group_column, AGG_FUNC(other_column)

FROM table_name

GROUP BY group_column;

✅ The column you’re grouping by must appear in both:


●​ The SELECT statement​

●​ The GROUP BY clause​

🧪 Example 1: Group by one field


SELECT certification, AVG(duration) AS avg_duration

FROM films

GROUP BY certification;

📌 This shows the average duration for each certification (PG, R, etc.)
🧪 Example 2: Count number of movies per language
SELECT language, COUNT(title) AS movie_count

FROM films

GROUP BY language;
❌ Common Error:
SELECT certification, title

FROM films

GROUP BY certification;

This will cause an error because title is not aggregated and not in the
GROUP BY.

✅ Fix:
SELECT certification, COUNT(title) AS num_titles

FROM films

GROUP BY certification;

🔸 3. GROUP BY Multiple Columns


You can group by more than one column by separating them with commas.

SELECT certification, language, COUNT(title) AS count_titles

FROM films

GROUP BY certification, language;

📌 This gives you a separate count for each unique combination of


certification and language.

🔸 4. Combining GROUP BY with ORDER BY


You can sort the grouped results using ORDER BY.

SELECT certification, COUNT(title) AS title_count

FROM films

GROUP BY certification

ORDER BY title_count DESC;

✅ This gives the most common certification at the top.


🔁 5. Order of Execution (Updated)
1.​ FROM​

2.​ WHERE (if filtering before grouping)​

3.​ GROUP BY​

4.​ SELECT​

5.​ ORDER BY​

6.​ LIMIT​

⛔ Note: You can refer to aliases (like title_count) in ORDER BY, but
not in WHERE.

✅ Summary Table
Keyword Description
GROUP BY Groups rows with same value in one or
more columns

COUNT() Aggregates data within each group

AVG(), Useful to calculate averages or totals per


SUM() group

ORDER BY Sorts grouped results

Error to Don’t SELECT non-aggregated columns


avoid not in GROUP BY

🎯 Final Tip
Always ask:

“Am I summarizing something per category?”

If yes — use GROUP BY.

Let me know if you'd like to practice grouping real-life cases like:

●​ Customers by country​

●​ Sales by product​

●​ Movies by release year and genre​


Or want me to give you a mini challenge!

Absolutely! Let’s walk through this SQL query step by step and explain
what it does, why it works, and where each part fits in the SQL logic.

✅ Query:
SELECT release_year, COUNT(title) AS film_count

FROM films

GROUP BY (release_year);

🔍 What This Query Does:


It returns a list of:

●​ Each unique release_year in the films table​

●​ The number of films (film_count) released in each of those years​

🧠 Detailed Explanation of Each Part:


🔸 SELECT release_year, COUNT(title) AS film_count
This is asking SQL to:

●​ Show the release year from each group​

●​ Count how many title entries (films) are in that year​


●​ Rename that count column as film_count (this is aliasing)​

📝 COUNT(title) only counts non-NULL title values, so if a


row has a missing title, it won’t be counted.

🔸 FROM films
📂
This tells SQL:​
“We’re working with the films table.”

🔸 GROUP BY (release_year)
This is the most important line:

●​ It tells SQL to group all rows that have the same release year into
one group.​

●​ Then, the COUNT(title) is calculated for each of those groups.​

✔️ So instead of counting across the full table, SQL counts per year.
🎯 For example, if there are:
●​ 10 films from 2010​

●​ 8 films from 2011​


The result will be:​

2010 | 10

2011 | 8
🔸 Optional Style Tip:
You don’t need the parentheses in GROUP BY (release_year) — this
also works:

GROUP BY release_year;

✅ It’s cleaner and preferred style.


🧪 Output Example (Let’s say your data has this):
release_y film_co
ear unt

2005 15

2006 21

2007 18

📊 Use Cases of This Query:


This kind of query is useful for:

●​ Checking how many films were released each year​

●​ Visualizing trends (like dips or spikes in film production)​

●​ Preparing data for line charts or bar graphs (for reporting)​


🔁 Order of Execution Behind the Scenes:
1.​ FROM films → Start with the table​

2.​ GROUP BY release_year → Cluster rows into groups per year​

3.​ COUNT(title) → Count rows per group​

4.​ SELECT → Return the grouped year and count​

5.​ (Optional): ORDER BY or LIMIT comes afterward​

✅ Final Version (with optional ordering):


SELECT release_year, COUNT(title) AS film_count

FROM films

GROUP BY release_year

ORDER BY film_count DESC;

This will show you the years with the most film releases at the top.

Would you like to now:

●​ Group by genre or certification?​

●​ Combine this with WHERE to filter before grouping (e.g., after 2000)?​
Let me know — happy to guide you!​
-- Find the release_year and average duration of films for each
year

SELECT release_year, AVG(duration) AS avg_duration

FROM films

GROUP BY release_year

Great job working with grouping and ordering in SQL! Let's break down
your query step-by-step so you understand exactly what it's doing, why
it’s correct, and how each part fits together.

✅ Your SQL Query


SELECT release_year, country, MAX(budget) AS max_budget

FROM films

GROUP BY release_year, country

ORDER BY release_year, country;

🔍 What This Query Does (In Simple Words):


This query gives you:

●​ The maximum film budget (MAX(budget))​

●​ For each unique combination of release_year and country​

●​ And then orders the result so you can read it by year, then by
country​
🧠“What
Think of it as asking:​
was the most expensive film made in each country every
year?”

🧩 Line-by-Line Breakdown
🔸 SELECT release_year, country, MAX(budget) AS max_budget
👉 What this does:
●​ Selects 3 fields:​

○​ release_year: the year the film was released​

○​ country: the country the film was produced in​

○​ MAX(budget): the largest budget found in each group​

●​ AS max_budget: gives a clear, friendly name to the result column.​

🔸 FROM films
You're pulling this data from the films table.

🔸 GROUP BY release_year, country


👉 What this does:
●​ This groups your data first by year, and then within each year, by
country​

●​ So:​
○​ All films from 2010, USA go into one group​

○​ All films from 2010, UK go into another group​

○​ All films from 2011, India go into another group​

○​ And so on...​

Then SQL applies MAX(budget) to each of these groups to find the


most expensive film per group.

✅ Grouping by multiple columns is very common when you're looking


at more specific combinations (e.g. "year + region", "category + product",
etc.)

🔸 ORDER BY release_year, country


This just sorts the final result:

●​ First by release_year in ascending order (default)​

●​ Then by country in alphabetical order​

So your output will be neat and easy to scan — year by year, country by
country.

🧪 Example Output Might Look Like:


release_y count max_bud
ear ry get
2005 Franc 45,000,00
e 0

2005 USA 120,000,0


00

2006 Japan 60,000,00


0

2006 USA 130,000,0


00

✅ Why This Query Is Correct


●​ You grouped by every non-aggregated field in the SELECT statement
(release_year, country) — this is required.​

●​ You used an aggregate function MAX(budget) to get a meaningful


summary per group​

●​ You used ORDER BY at the end to make the output more readable​

📊 Optional Improvement
Want to see only the top 10 most expensive combinations? Add LIMIT:

...

ORDER BY max_budget DESC


LIMIT 10;

✅ Summary Table
Clause Role

SELECT Choose what to display (grouped columns +


aggregate function)

MAX(bud Gets the highest budget in each group


get)

GROUP Groups data by unique year-country combinations


BY

ORDER Sorts the final output for easier interpretation


BY

Let me know if you'd like to:

●​ Add WHERE to filter by specific countries or years​

●​ Turn this into a visualization (e.g., country vs budget bar graph)​

●​ Or practice with similar multi-group examples!


●​ ​
SELECT release_year, COUNT(DISTINCT language) AS lang_diversity

FROM films

GROUP BY release_year

ORDER BY lang_diversity DESC;

Absolutely! Here's a clear and easy explanation of the concept of


filtering grouped data in SQL using HAVING, including theory +
examples + comparison to WHERE + visual memory tricks. This is a
crucial SQL skill — you're almost at a pro level now!

🧾 SQL Topic: Filtering Grouped Data with HAVING


🧠 Why Can’t We Use WHERE with Aggregate Functions?
👉
Because of the order of execution in SQL.​
WHERE happens before grouping and aggregation.​
So SQL doesn’t yet know the values you're trying to filter (like
AVG(duration) or COUNT(title)).

🔄 Order of Execution in SQL (Behind the Scenes)


Ste Keywo Description
p rd

1️⃣ FROM Choose the table


2️⃣ WHERE Filter rows (before any
grouping)

3️⃣ GROUP Group the data


BY

4️⃣ HAVIN Filter groups (after


G aggregation)

5️⃣ SELEC Select fields, do


T calculations

6️⃣ ORDER Sort results


BY

7️⃣ LIMIT Trim final result

✅ That's why HAVING is used for grouped data only — when you're
filtering after aggregation.

📊 Comparison: WHERE vs HAVING


Use WHERE when... Use HAVING when...

You want to filter rows before You want to filter groups after
grouping aggregation
You’re filtering raw columns (e.g. You’re filtering summary values
genre, year) (e.g. COUNT())

✅ Examples:
🔹 Example 1: Using WHERE
📌 Question: “Which films were released in 2000?”
SELECT title

FROM films

WHERE release_year = 2000;

🎯 You're filtering individual rows (films).


🔹 Example 2: Using HAVING
📌 Question: “Which years had more than 10 films released?”
SELECT release_year, COUNT(title) AS film_count

FROM films

GROUP BY release_year

HAVING COUNT(title) > 10;

🎯 You first group by year, then filter the groups using HAVING.
🔹 Example 3: Average duration over 2 hours
📌 Question: “Which years had films with average duration > 120
minutes?”

SELECT release_year, AVG(duration) AS avg_duration

FROM films

GROUP BY release_year

HAVING AVG(duration) > 120;

✅ You can’t use WHERE here because AVG(duration) doesn’t exist yet
when WHERE runs.

🧠 Helpful Visual:
Think of SQL as going through a filtering pipeline:

●​ 👀 Step 1: “Filter individual items” → Use WHERE​


●​ 🧹 Step 2: “Group them into buckets” → Use GROUP BY​

●​ 🧮 Step 3: “Apply summary logic per bucket” → Use HAVING​

🧪 Bonus: Combine WHERE + GROUP BY + HAVING


📌 Question: “Among PG and G-rated movies, which ratings have more
than 500 films?”

SELECT certification, COUNT(title) AS film_count


FROM films

WHERE certification IN ('G', 'PG')

GROUP BY certification

HAVING COUNT(title) > 500;

🔑 Summary Cheat Sheet


Concept Keyword Example

Filter raw rows WHERE WHERE


release_year =
2000

Group rows GROUP BY GROUP BY


together certification

Filter grouped HAVING HAVING


data COUNT(title) >
10

Use aggregate in ✅
Only with HAVING
filter HAVING AVG(duration) >
120
Would you like practice questions or quiz-style mini challenges to test
your understanding? Just say the word!
-- Select the country and distinct count of certification as
certification_count

SELECT country, COUNT(DISTINCT certification) AS


certification_count

FROM films

-- Group by country

GROUP BY country

-- Filter results to countries with more than 10 different


certifications

HAVING COUNT(DISTINCT certification) > 10;

You're doing great — and this query is 100% correct! 🎉 ​


Let’s walk through it step-by-step so you fully understand how it works and
why each line is important.

✅ Full Query Recap:


SELECT country, COUNT(DISTINCT certification) AS certification_count

FROM films

GROUP BY country

HAVING COUNT(DISTINCT certification) > 10;

🔍 What This Query Does:


You’re asking SQL to:

1.​ Look at each country in the films table,​

2.​ Count how many unique film certifications (e.g., G, PG, R) exist
per country,​

3.​ Only show countries where this number is more than 10.​

🧩 Breakdown of Each Part:


🔹 SELECT country, COUNT(DISTINCT certification) AS
certification_count

●​ You are selecting:​

○​ The country​

○​ The number of distinct certifications for that country​

●​ AS certification_count is an alias so your output column has


a meaningful name​

🔹 FROM films
●​ You're pulling the data from the films table​

🔹 GROUP BY country
●​ Groups all rows per country so that you can:​

○​ Count things within each country group​

🔹 HAVING COUNT(DISTINCT certification) > 10


●​ Filters the grouped results (not individual rows)​

●​ You only want to see countries where there are more than 10
different certifications​

🔸Because
Why HAVING instead of WHERE?​
you're filtering on an aggregate function
(COUNT(DISTINCT certification)), and HAVING is used
for grouped data.

🧪 Example Output:
count certification_c
ry ount

USA 14

India 12

Franc 11
e

🌍 Other countries with fewer than 10 unique certifications will be


excluded from this result.
✅ Summary
Claus Purpose
e

SELEC Choose country and count of unique


T certifications

FROM Use the films table

GROUP Group by country to summarize each


BY group

HAVIN Filter grouped results based on the


G count (> 10)

AS Rename the result column for clarity

Let me know if you'd like to:

●​ Sort these results by highest certification count (ORDER BY


certification_count DESC)​

●​ Filter specific countries or years before grouping​

●​ Or get a visual version of this with sample data!​


-- Select the country and average_budget from films

SELECT country, ROUND(AVG(budget), 2) AS average_budget

FROM films

-- Group by country

GROUP BY country

-- Filter to countries with an average_budget of more than one


billion

HAVING AVG(budget) > 1000000000

-- Order by descending order of the aggregated budget

ORDER BY average_budget DESC;

EXERCISE : IMPORTANT

Great question! You're really thinking like a SQL pro now. Let’s walk
through why you only selected release_year and didn’t include
title in this query — and what would happen if you tried to add it.

✅ Your Query:
SELECT release_year

FROM films

GROUP BY release_year

HAVING release_year > 1990;

📌 What it does:
●​ Groups the films by release_year​

●​ Filters those groups to show only years after 1990​

❓Why not add title to the SELECT?


Because you're grouping by release_year only, the only columns
you’re allowed to SELECT are:

1.​ Columns in the GROUP BY clause (release_year)​

2.​ Aggregated columns like COUNT(title) or MAX(duration), etc.​

❌ What happens if you add title without aggregation?


SELECT release_year, title

FROM films

GROUP BY release_year

HAVING release_year > 1990;

This will cause an SQL error like:

"column 'title' must appear in the GROUP BY clause or be used in


an aggregate function"

Why?

Because SQL doesn’t know which title to show — each year might
have many titles, but SQL wants to return one row per group.
✅ What you CAN do:
Option 1: Count titles per year

SELECT release_year, COUNT(title) AS film_count

FROM films

GROUP BY release_year

HAVING release_year > 1990;

✅ Now you're saying:


"Give me each year (after 1990), and tell me how many films
were released that year."

Option 2: Show one title per year (not recommended)

SELECT release_year, MIN(title) AS first_title

FROM films

GROUP BY release_year

HAVING release_year > 1990;

📌 This forces SQL to pick one title per year using an aggregate (MIN()),
but it's not ideal unless you have a reason for it.

🧠 In Simple Terms:
You want to... Then you must...
Show group column (like year) Include it in GROUP BY

Show detail column (like title) Use an aggregate like


COUNT(), MIN(), etc.

Use HAVING with aggregates or ✅ Allowed (after grouping is


done)
group keys

Let me know if you want to practice grouping + aggregation using movie


data, or add filters like WHERE genre = 'Action' before grouping!
SELECT release_year, AVG(budget) AS avg_budget, AVG(gross) AS
avg_gross

FROM films

WHERE release_year > 1990

GROUP BY release_year

HAVING AVG(budget) > 60000000

-- Order the results from highest to lowest average gross and


limit to one

ORDER BY avg_gross DESC

lIMIT 1;

🎉 Congratulations on completing the course! You’ve covered a lot, and


it’s totally normal to take a moment to celebrate. Here’s a clean,
structured summary of what you’ve learned and what it means in
real-world SQL terms — perfect for reviewing, adding to LinkedIn, or even
using in job interviews.
✅ SQL Summary: What You’ve Learned
🔹 Chapter 1: Basics of Querying
●​ SELECT, FROM: Used to pull data from a table​

●​ COUNT(): Counts how many values (or rows) exist​

●​ LIMIT: Restricts how many results to show (useful for testing)​

🔹 Chapter 2: Filtering Data


●​ WHERE: Filters individual rows​

●​ Comparison operators: =, !=, >, <, >=, <=​

●​ Text filtering: LIKE, NOT LIKE, IN​

●​ Missing values: IS NULL, IS NOT NULL​

🔹 Chapter 3: Summarizing & Cleaning


●​ Aggregate functions:​

○​ AVG(), SUM(), MIN(), MAX(), COUNT()​

●​ ROUND(): Used to clean and control decimal output​

●​ Arithmetic: Basic math (+ - * /) inside queries​

🔹 Chapter 4: Organizing and Grouping


●​ ORDER BY: Sort results alphabetically or numerically​
●​ DESC, ASC: Sort direction​

●​ GROUP BY: Summarize data per category (e.g., per year or country)​

●​ HAVING: Filters groups, used after aggregation​

🔧 Practical Skills Gained


You now know how to:

●​ Query and explore any relational database​

●​ Filter and extract meaningful records​

●​ Handle missing or messy data​

●​ Perform numeric calculations inside SQL​

●​ Group and summarize data like a pro analyst​

●​ Sort and present your results in clean order​

●​ Debug errors and follow best style practices​

🚀 What’s Next?
If you want to level up:

●​ Joins & Relationships: Learn to combine data from multiple tables​

●​ Window Functions: For advanced analytics (e.g., rankings, rolling


averages)​
●​ Projects: Start building mini dashboards or reports (e.g., in
DataCamp Workspace, Power BI, etc.)​

●​ SQL on your resume: Add bullet points like:​




▪ Proficient in SQL querying: filtering, grouping, sorting, and
summarizing datasets using aggregate functions and logical
operators​
▪ Comfortable with debugging, NULL handling, and clean query
writing​

Would you like:

●​ A LinkedIn-ready post summarizing this?​

●​ A mock interview SQL question set?​

●​ Or a portfolio-style SQL project idea?​

Just say the word — I’m here to help you take the next step! 🚀

You might also like