Ever wondered how Netflix knows exactly what show you might binge next? That magic starts with a language called SQL.
SQL (pronounced "sequel") is a way to talk to a database. Think of a database as a giant, super‑organized spreadsheet, and SQL as the set of instructions you give to pull out the rows you need.
What is SQL?
SQL stands for Structured Query Language. It’s the standard language used to manage and retrieve data from relational databases (databases that store information in tables linked by relationships). You can think of a table like a spreadsheet tab – rows are records, columns are fields.
Why does CBSE Class 12 care about SQL?
The board wants you to understand how real‑world apps store data. Knowing SQL lets you create, modify, and query databases – a skill that shows up in exams and future tech jobs.
Basic SQL Commands You Must Know
- SELECT: pick columns you want to see.
- FROM: tell SQL which table to look in.
- WHERE: filter rows based on a condition.
- INSERT: add a new row.
- UPDATE: change existing data.
- DELETE: remove rows.
Writing Your First Query
Imagine a table students with columns roll_no, name, marks. To list the names of students scoring above 80, you’d write:
SELECT name FROM students WHERE marks > 80;
SQL reads this like a recipe: first, select the name column, then look from the students table, and finally keep only those rows where marks are greater than 80.
Creating a Table – The DDL Part
DDL (Data Definition Language) commands shape the structure of your database. To create the students table:
CREATE TABLE students (\n roll_no INT PRIMARY KEY,\n name VARCHAR(30),\n marks INT\n);
INT means integer (whole number). VARCHAR(30) means a short text up to 30 characters.
SQL Command Categories – Quick Comparison
| Category | Purpose | Typical Commands |
|---|---|---|
| DDL | Define or change database structure | CREATE, ALTER, DROP |
| DML | Manipulate the data inside tables | INSERT, UPDATE, DELETE, SELECT |
| DCL | Control access to data | GRANT, REVOKE |
Step‑by‑Step Flow for Building a Simple SELECT Query
Common Mistakes to Avoid
- Missing commas between column names.
- Using single quotes for column names instead of backticks (or none at all).
- Forgetting the semicolon at the end – many DB tools need it.
- Mixing up
ANDandORwithout parentheses, which changes the logic.
Mini Practice: Write the Query
Given a table books(book_id, title, author, price), list all titles by "J.K. Rowling" priced below 500.
SELECT title FROM books WHERE author = 'J.K. Rowling' AND price < 500;
📝 Likely Exam Questions
- Define SQL and mention two types of SQL commands.
SQL is Structured Query Language, used to manage relational databases. Two types are DDL (e.g., CREATE) and DML (e.g., SELECT). - Write an SQL statement to create a table named
employeeswith columnsemp_id(integer primary key),emp_name(string up to 40 chars), andsalary(integer).CREATE TABLE employees (\n emp_id INT PRIMARY KEY,\n emp_name VARCHAR(40),\n salary INT\n);
- Given a table
sales(sale_id,product,amount), retrieve theproductnames whereamountis greater than 1000, sorted descending.SELECT product FROM sales WHERE amount > 1000 ORDER BY amount DESC;
- Explain the difference between
DELETEandTRUNCATEstatements.DELETEremoves rows one by one and can be rolled back; it can have aWHEREclause.TRUNCATEremoves all rows instantly, cannot be filtered, and usually cannot be rolled back. - What does the
PRIMARY KEYconstraint enforce?
It ensures each row has a unique, non‑null value in that column, acting like an identifier.