Imagine you have a giant magic notebook ๐. Every time someone plays a game or buys a toy, the notebook writes it down. SQL is the magic language you use to talk to this notebook and ask it questions! Ready to learn the magic words? โจ
Every big app you useโlike Roblox, YouTube, or Instagramโhas a database to remember things. SQL is the hero that saves and finds all that data! ๐ฆธโโ๏ธ
It remembers millions of things without forgetting anything!
It can find exactly what you want in less than a second!
Every video game and website uses SQL to save your progress.
You can ask it to find secrets hidden in mountains of data!
In SQL, data is stored in Tables (they look just like Excel sheets!). Let's look at a table called students. We will use this table for all our fun examples! ๐
| id | name | age | course | grade |
|---|---|---|---|---|
| 1 | Aarav | 13 | Robotics | A |
| 2 | Priya | 12 | Art | B+ |
| 3 | Rahul | 14 | Robotics | A- |
| 4 | Neha | 13 | Music | B |
To get data out, we use the magic word SELECT. The star * means "everything". So SELECT * FROM students; means "Give me everything from the students table!" ๐
-- Get everything from the table
SELECT * FROM students;
-- Get only the names and ages
SELECT name, age FROM students;
--- Result 1 --- id | name | age | course | grade 1 | Aarav | 13 | Robotics | A 2 | Priya | 12 | Art | B+ 3 | Rahul | 14 | Robotics | A- 4 | Neha | 13 | Music | B --- Result 2 --- name | age Aarav | 13 Priya | 12 Rahul | 14 Neha | 13
-- are used to write comments. The computer ignores them, but they help humans understand the code!What if you only want to see students who are in "Robotics"? You use the WHERE clause! It acts like a detective's magnifying glass ๐, filtering out only the rows you want.
-- Find only the Robotics students
SELECT name, course
FROM students
WHERE course = 'Robotics';
name | course Aarav | Robotics Rahul | Robotics
Imagine sorting your Pokemon cards by their power. ORDER BY does exactly that! You can sort from A to Z (ASC) or Z to A (DESC). ๐
-- Sort students by age, oldest first (DESC)
SELECT name, age
FROM students
ORDER BY age DESC;
name | age Rahul | 14 Aarav | 13 Neha | 13 Priya | 12
A new student joined the class! We need to add them to our magic notebook. We use INSERT INTO to glue a new row into the table. ๐ฅ
-- Add a new student named Vikram
INSERT INTO students (id, name, age, course, grade)
VALUES (5, 'Vikram', 13, 'Art', 'A+');
-- Let's check if he was added!
SELECT * FROM students;
โ 1 row added. id | name | age | course | grade 1 | Aarav | 13 | Robotics | A 2 | Priya | 12 | Art | B+ 3 | Rahul | 14 | Robotics | A- 4 | Neha | 13 | Music | B 5 | Vikram | 13 | Art | A+
Oops! Priya just had a birthday and turned 13. We need to erase the 12 and write 13. We use UPDATE to change data that is already in the table. ๐
-- Change Priya's age to 13
UPDATE students
SET age = 13
WHERE name = 'Priya';
-- Let's check Priya's new age
SELECT name, age FROM students
WHERE name = 'Priya';
โ 1 row updated. name | age Priya| 13
WHERE with UPDATE! If you forget it, you might accidentally change EVERYONE'S age to 13!If a student leaves the school, we need to remove them. We use DELETE to completely erase a row. It's like tearing up an old page! ๐๏ธ
-- Remove Rahul from the table
DELETE FROM students
WHERE name = 'Rahul';
-- Let's see who is left
SELECT name FROM students;
โ 1 row deleted. name Aarav Priya Neha Vikram
Let's put all the magic words together! We will add a student, update a grade, delete someone, and then look at our final list. This is how real apps are made! ๐
-- 1. Add a new student
INSERT INTO students (id, name, age, course, grade)
VALUES (6, 'Sneha', 14, 'Music', 'B');
-- 2. Update Aarav's grade to A+
UPDATE students
SET grade = 'A+'
WHERE name = 'Aarav';
-- 3. Delete Neha
DELETE FROM students
WHERE name = 'Neha';
-- 4. Show the final class, sorted by name!
SELECT name, course, grade
FROM students
ORDER BY name ASC;
โ 1 row added. โ 1 row updated. โ 1 row deleted. --- Final Class List --- name | course | grade Aarav | Robotics | A+ Priya | Art | B+ Sneha | Music | B Vikram | Art | A+
WHERE name = 'Neha' in step 3? (Answer: Everyone would get deleted!)Let's see how much you learned! Pick the best answer. ๐ง
Check the boxes as you finish each lesson. Watch your progress bar fill up! ๐
Awesome job reaching here! ๐ You just learned the basics of SQL. Here are fun things to do next: