103.68K
AI

SQL_Full_Lesson_Presentation

1.

SQL
From Database Basics to Real Queries
A step-by-step lesson with real-life examples
CORE IDEA
SQL lets us ask questions about structured data — and get useful answers quickly.
1

2.

Learning Goals
By the end of the lesson, you should be able to...
• Explain what a database, table, row, column, primary key, and foreign key are.
• Understand why SQL exists and what problems it solves.
• Write basic queries with SELECT, FROM, WHERE, ORDER BY, and LIMIT.
• Add, change, and remove data with INSERT, UPDATE, and DELETE.
• Summarize data with COUNT, SUM, AVG, GROUP BY, and HAVING.
• Combine tables with JOIN and read a real-world query step by step.
IMPORTANT
SQL is not about memorizing commands. It is about learning how to ask precise questions about data.
2

3.

1. What Is Data?
Start with something familiar: a school, shop, or social network.
REAL LIFE
IN A DATABASE
• Student name, age, class
• Data is stored in a structured way.
• Product name, price, stock
• Related information can be connected.
• Customer email, city, orders
• Software can search and analyze it.
WHY IT MATTERS
A database turns a huge collection of facts into information we can find, update, and analyze.
3

4.

2. What Is a Database?
Think of it as an organized digital storage system.
• A database stores information in a structured form.
• A database can contain many tables.
• Different tables can represent different parts of a business.
• Example: an online shop may have Customers, Products, Orders, and Payments.
REAL-LIFE ANALOGY
A database is like a well-organized library: books are stored in sections, and a catalog helps you find exactly what you need.
4

5.

3. What Is a Table?
A table stores one type of related information.
ID
Name
Age
Email
1
Alice
19
alice@example.com
2
Bob
20
bob@example.com
3
Maria
19
maria@example.com
4
David
21
david@example.com
TABLE = COLLECTION OF RELATED RECORDS
For example, one Students table can contain all students in a school.
5

6.

4. Rows, Columns, and Values
These three terms are essential.
COLUMN
ROW
• Describes one attribute.
• One complete record.
• Examples: name, age, email.
• Example: one student.
• Usually has one data type.
• Also called a record.
MEMORY TRICK
Column = WHAT we store. Row = WHO/WHICH record we store.
6

7.

5. Keys: How Tables Identify Data
Keys help databases keep records unique and connected.
PRIMARY KEY
FOREIGN KEY
A column (or set of columns) that uniquely identifies each row. A value that points to a row in another table.
Example: student_id = 1042.
Example: orders.customer_id → customers.id.
REAL LIFE
A passport number identifies one person. A customer ID identifies one customer in an online shop.
7

8.

6. What Is SQL?
Structured Query Language
• SQL is a language used to communicate with relational databases.
• SQL can read data, add data, change data, delete data, and define database
structures.
• SQL describes WHAT you want; the database system decides HOW to execute
it.
IMPORTANT
SQL = asking the database a precise question or giving it a precise instruction.
8

9.

7. Your First SQL Query
SELECT tells the database which columns you want.
SELECT name, age
FROM students;
• SELECT → what information do I want?
• FROM → which table contains it?
• ; → marks the end of the statement.
IN PLAIN ENGLISH
“Give me the name and age of every student.”
9

10.

8. SELECT * — All Columns
The asterisk * means “all columns”.
SELECT *
FROM students;
USEFUL FOR LEARNING
BE CAREFUL
It is convenient when exploring a table for the first time.
In production systems, selecting only the columns you need is oft
10

11.

9. WHERE — Filter the Data
WHERE answers: “Which rows do I want?”
SELECT name, age
FROM students
WHERE age >= 20;
• Only rows that satisfy the condition are returned.
• Common operators: =, >, <, >=, <=, <>.
• Text values normally use single quotes: WHERE city = 'Almaty'.
11

12.

10. Combining Conditions
Real questions often need more than one condition.
SELECT name, city
FROM customers
WHERE city = 'Almaty'
AND age >= 18;
AND
OR
•B
•A
•o
•t
•t
•h
•l
•e
12

13.

11. ORDER BY and LIMIT
Sort the result and control how many rows you see.
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 5;
• ORDER BY price DESC → most expensive first.
• ASC → ascending order (small → large).
• DESC → descending order (large → small).
• LIMIT 5 → show only five rows.
13

14.

12. INSERT — Add New Data
INSERT creates a new row.
INSERT INTO students (name, age, email)
VALUES ('Emma', 20, 'emma@example.com');
REAL LIFE
A new student enrolls → the school system needs to add a new student record.
14

15.

13. UPDATE and DELETE
Changing and removing data requires precision.
UPDATE
DELETE
UPDATE students
DELETE FROM students
SET age = 21
WHERE id = 2;
WHERE id = 2;
CRITICAL
Never casually run UPDATE or DELETE without checking the WHERE condition. Without it, you may change or delete many rows.
15

16.

14. Aggregate Functions
Turn many rows into useful numbers.
COUNT
SUM / AVG
•H
•T
•o
•o
•w
•t
•a
•m
•l
•a
•n
•o
•y
•r
•?
•a
TOOLS
•MORE
C
→ smallest
•MIN(value)
O
→ largest
•MAX(value)
U
•v
•e
•r
•a
16

17.

15. GROUP BY — Summarize by Category
GROUP BY creates one result group for each category.
SELECT city, COUNT(*) AS customers
FROM customers
GROUP BY city;
RESULT
LIFE EXAMPLE
Almaty → 120 customers
Instead of asking “How many customers exist?”, ask “How many c
Astana → 95 customers
Shymkent → 63 customers
17

18.

16. HAVING — Filter Groups
WHERE filters rows. HAVING filters grouped results.
SELECT city, COUNT(*) AS customers
FROM customers
GROUP BY city
HAVING COUNT(*) > 100;
KEY DIFFERENCE
QUESTION ANSWERED
WHERE → before grouping
“Which cities have more than 100 customers?”
HAVING → after grouping
18

19.

17. JOIN — Connect Tables
Relational databases store related information in separate tables.
SELECT customers.name, orders.total
FROM customers
JOIN orders
ON customers.id = orders.customer_id;
• customers.id identifies the customer.
• orders.customer_id points back to that customer.
• JOIN combines matching rows so we can answer richer questions.
19

20.

18. JOIN in Real Life
Imagine an online shop.
CUSTOMERS
ORDERS
• ID: 17
• Order: #5008
• Name: Alex
• Customer ID: 17
• City: Almaty
• Total: $85
JOIN QUESTION
“What is Alex's order total?” → connect the two tables through customer ID.
20

21.

19. How to Read a Query
SQL has a written order and a logical processing idea.
WRITTEN STRUCTURE
MENTAL MODEL
SELECT
1. Choose the data source.
FROM
2. Connect tables if needed.
JOIN
3. Filter rows.
WHERE
4. Group and calculate.
GROUP BY
5. Filter groups.
HAVING
6. Sort the final result.
ORDER BY
7. Limit the output.
LIMIT
21

22.

20. Full Real-World Example
Question: “Which cities generated more than $10,000 in sales?”
SELECT c.city, SUM(o.total) AS sales
FROM customers AS c
JOIN orders AS o ON c.id = o.customer_id
GROUP BY c.city
HAVING SUM(o.total) > 10000
ORDER BY sales DESC;
WHAT IT DOES
Connect customers → orders → group by city → calculate sales → keep cities above $10,000 → sort from highest to lowest.
22

23.

21. Practice: Your Turn
Try to write the SQL before looking at the answer.
• 1. Show all product names and prices.
• 2. Show products costing more than 100.
• 3. Show the 5 cheapest products.
• 4. Count how many customers are in each city.
• 5. Find cities with more than 50 customers.
• 6. Show each customer's name and their order total.
THINK FIRST
For each task, identify: SELECT → FROM → WHERE/JOIN → GROUP BY → ORDER BY/LIMIT.
23

24.

22. Practice Answers
Compare your reasoning with these solutions.
-- 1
SELECT name, price FROM products;
-- 2
SELECT name, price FROM products WHERE price > 100;
-- 3
SELECT name, price FROM products ORDER BY price ASC LIMIT 5;
-- 4
SELECT city, COUNT(*) FROM customers GROUP BY city;
24

25.

23. SQL Best Practices
Good SQL is clear, safe, and easy to maintain.
• Use meaningful table and column names.
• Select only the columns you need.
• Format queries with indentation and line breaks.
• Always verify the WHERE condition before UPDATE or DELETE.
• Use aliases to make JOIN queries easier to read.
• Test a SELECT version before making a large data change.
PRO TIP
Write SQL so another person can understand your intention without asking you to explain it.
25

26.

24. SQL Cheat Sheet
The commands to remember first
SELECT
FROM
WHERE
Choose columns
Choose table
Filter rows
ORDER BY
LIMIT
INSERT
Sort results
Limit rows
Add rows
UPDATE
DELETE
GROUP BY
Change rows
Remove rows
Create groups
HAVING
JOIN
Filter groups
Connect tables
REMEMBER
SQL becomes easy when you learn to translate a real question into SELECT + FROM + conditions + grouping + sorting.
26

27.

25. Final Takeaway
From data to answers
THE CORE SKILL
Take a real-world question → identify the data → write the SQL → inspect the result → refine the question.
• Databases store structured information.
• Tables contain rows and columns.
• Keys identify and connect records.
• SQL lets us retrieve, analyze, and modify data.
• Start simple. Add complexity only when the question requires it.
NEXT STEP
Practice by creating a small database for a school, shop, library, or café — then ask it real questions.
27
English     Русский Rules