Complete SQL Masterclass Using Shah Rukh Khan Movies
We’ll cover SQL from absolute beginner to advanced professional level, using Shah Rukh Khan movies as our practical database project.
https://www.twincafe.com/nagendra-k?referral=KhyOL_UQ
Instead of learning SQL through boring examples, imagine you are building a real movie database like IMDb.
By the end, you will understand how to create databases, insert records, search movies, calculate collections, connect tables, write advanced queries, and manage a complete database.
Shah Rukh Khan’s Jawan Will Release In Japan In November: Here’s All We Know | Herzindagi
Download YRF Pathaan Poster Wallpaper | Wallpapers.com
4
1. Complete course syllabus
SQL learning roadmap
Beginner to Advanced
1
Module 1 – SQL Fundamentals
Database, tables, rows, columns, SQL commands, data types
2
Module 2 – Database Creation
CREATE DATABASE, USE, CREATE TABLE, DROP, ALTER, TRUNCATE
3
Module 3 – Insert and Retrieve
INSERT, SELECT, DISTINCT, WHERE, LIMIT
4
Module 4 – Filtering and Sorting
AND, OR, NOT, IN, BETWEEN, LIKE, ORDER BY
5
Module 5 – SQL Functions
COUNT, SUM, AVG, MIN, MAX, UPPER, LOWER, ROUND
6
Module 6 – Grouping
GROUP BY, HAVING, aggregate functions
7
Module 7 – Relationships and JOINs
Primary keys, foreign keys, INNER, LEFT, RIGHT, CROSS JOIN
8
Module 8 – Advanced SQL
Subqueries, CASE, UNION, EXISTS, CTEs, window functions
9
Module 9 – Database Management
Views, indexes, procedures, triggers, transactions
10
Module 10 – Professional Project
Complete SRK Movie Database with 100 practice queries
PART 1 – SQL fundamentals
2. What is SQL?
SQL means Structured Query Language.
It is used to communicate with databases.
Example: You have 100 SRK movies in your database. You want to find all movies released in 2023.
You write:
SELECT * FROM Movies
WHERE release_year = 2023;
The database returns matching movies.
3. Understand database structure
Imagine this table:
Movies
| movie_id | movie_name | year | rating |
|---|---|---|---|
| 1 | Jawan | 2023 | 7.0 |
| 2 | Pathaan | 2023 | 5.8 |
| 3 | Chennai Express | 2013 | 6.0 |
| 4 | Dilwale | 2015 | 5.1 |
| 5 | Dunki | 2023 | 6.5 |
- Database: Collection of tables.
- Table: Collection of related records.
- Row: One movie record.
- Column: A particular property, such as movie name.
- Primary key: A unique identifier for a record.
- Foreign key: A column connecting one table to another.
4. SQL command categories
| Category | Purpose | Commands |
|---|---|---|
| DDL | Structure management | CREATE, ALTER, DROP, TRUNCATE |
| DML | Data modification | INSERT, UPDATE, DELETE |
| DQL | Data retrieval | SELECT |
| DCL | Access control | GRANT, REVOKE |
| TCL | Transaction management | COMMIT, ROLLBACK, SAVEPOINT |
PART 2 – Create your complete database
5. Create database
Input
CREATE DATABASE SRK_Movie_DB;
USE SRK_Movie_DB;
Output
Database created successfully.
Database changed.
6. Create the main movies table
CREATE TABLE Movies (
movie_id INT PRIMARY KEY AUTO_INCREMENT,
movie_name VARCHAR(100) NOT NULL,
release_year INT,
language VARCHAR(30),
budget_crore DECIMAL(10,2),
collection_crore DECIMAL(10,2),
rating DECIMAL(3,1)
);
Output
Table Movies created successfully.
7. Insert sample records
These are illustrative practice records. Financial figures are sample values, not verified box-office data.
INSERT INTO Movies
(movie_name, release_year, language,
budget_crore, collection_crore, rating)
VALUES
('Jawan', 2023, 'Hindi', 300, 1148, 7.0),
('Pathaan', 2023, 'Hindi', 250, 1050, 5.8),
('Chennai Express', 2013, 'Hindi', 115, 423, 6.0),
('Dilwale', 2015, 'Hindi', 165, 376, 5.1),
('Dunki', 2023, 'Hindi', 120, 470, 6.5),
('Raees', 2017, 'Hindi', 127, 287, 6.8),
('My Name Is Khan', 2010, 'Hindi', 85, 223, 7.9),
('Om Shanti Om', 2007, 'Hindi', 40, 215, 6.7);
Output
8 rows inserted.
8. Display all records
SELECT * FROM Movies;
Output
| ID | Movie | Year | Rating |
|---|---|---|---|
| 1 | Jawan | 2023 | 7.0 |
| 2 | Pathaan | 2023 | 5.8 |
| 3 | Chennai Express | 2013 | 6.0 |
| 4 | Dilwale | 2015 | 5.1 |
| 5 | Dunki | 2023 | 6.5 |
| 6 | Raees | 2017 | 6.8 |
| 7 | My Name Is Khan | 2010 | 7.9 |
| 8 | Om Shanti Om | 2007 | 6.7 |
PART 3 – All basic SQL commands
9. SELECT
Get specific columns.
SELECT movie_name, release_year
FROM Movies;
Output:
| movie_name | release_year |
|---|---|
| Jawan | 2023 |
| Pathaan | 2023 |
| Chennai Express | 2013 |
| Dilwale | 2015 |
| Dunki | 2023 |
| Raees | 2017 |
| My Name Is Khan | 2010 |
| Om Shanti Om | 2007 |
10. DISTINCT
Find unique release years.
SELECT DISTINCT release_year
FROM Movies
ORDER BY release_year;
Output:
| release_year |
|---|
| 2007 |
| 2010 |
| 2013 |
| 2015 |
| 2017 |
| 2023 |
11. WHERE
Find movies with ratings above 6.5.
SELECT movie_name, rating
FROM Movies
WHERE rating > 6.5;
Output:
| Movie | Rating |
|---|---|
| Jawan | 7.0 |
| Raees | 6.8 |
| My Name Is Khan | 7.9 |
| Om Shanti Om | 6.7 |
12. AND
SELECT * FROM Movies
WHERE release_year = 2023
AND rating > 6;
Output: Jawan and Dunki.
13. OR
SELECT * FROM Movies
WHERE release_year = 2010
OR release_year = 2023;
Output: Jawan, Pathaan, Dunki, My Name Is Khan.
14. NOT
SELECT * FROM Movies
WHERE NOT release_year = 2023;
Output: All movies except Jawan, Pathaan and Dunki.
15. BETWEEN
SELECT * FROM Movies
WHERE release_year BETWEEN 2010 AND 2020;
Output: Chennai Express, Dilwale, Raees, My Name Is Khan.
16. IN
SELECT * FROM Movies
WHERE release_year IN (2007, 2015, 2023);
Output: Om Shanti Om, Dilwale, Jawan, Pathaan, Dunki.
17. LIKE
Find movies whose names begin with D.
SELECT * FROM Movies
WHERE movie_name LIKE 'D%';
Output: Dilwale and Dunki.
Other LIKE patterns:
| Pattern | Meaning |
|---|---|
'D%' | Starts with D |
'%n' | Ends with n |
'%an%' | Contains an |
'_a%' | Second letter is a |
18. ORDER BY
SELECT movie_name, rating
FROM Movies
ORDER BY rating DESC;
Output: My Name Is Khan, Jawan, Raees, Om Shanti Om, Dunki, Chennai Express, Pathaan, Dilwale.
19. LIMIT and OFFSET
SELECT * FROM Movies
ORDER BY release_year DESC
LIMIT 3;
Output: The three latest-release-year records.
SELECT * FROM Movies
ORDER BY release_year
LIMIT 3 OFFSET 2;
Output: The third through fifth records in ascending year order.
20. UPDATE
UPDATE Movies
SET rating = 8.0
WHERE movie_name = 'Jawan';
Output:
1 row affected.
21. DELETE
DELETE FROM Movies
WHERE movie_name = 'Raees';
Output:
1 row deleted.
22. ALTER TABLE
Add a new column.
ALTER TABLE Movies
ADD COLUMN director VARCHAR(100);
Change a column type:
ALTER TABLE Movies
MODIFY COLUMN movie_name VARCHAR(150);
Rename a column:
ALTER TABLE Movies
RENAME COLUMN language TO movie_language;
23. TRUNCATE and DROP
TRUNCATE TABLE Movies;
Removes all rows but keeps the table structure.
DROP TABLE Movies;
Removes the table itself.
Important
These commands are destructive. Do not execute them on your working database without a backup.
PART 4 – SQL aggregate functions
Aggregate functions calculate information from multiple records.
Assume the original eight sample records are available.
24. COUNT()
Count total movies.
SELECT COUNT(*) AS total_movies
FROM Movies;
Output:
| total_movies |
|---|
| 8 |
25. SUM()
Calculate total sample collection.
SELECT SUM(collection_crore) AS total_collection
FROM Movies;
Output:
| total_collection |
|---|
| 4192 |
26. AVG()
Calculate average movie rating.
SELECT ROUND(AVG(rating), 2) AS average_rating
FROM Movies;
Output:
| average_rating |
|---|
| 6.48 |
27. MAX()
Find the highest-rated movie.
SELECT movie_name, rating
FROM Movies
WHERE rating = (
SELECT MAX(rating) FROM Movies
);
Output:
| movie_name | rating |
|---|---|
| My Name Is Khan | 7.9 |
28. MIN()
Find the lowest-rated movie.
SELECT movie_name, rating
FROM Movies
WHERE rating = (
SELECT MIN(rating) FROM Movies
);
Output:
| movie_name | rating |
|---|---|
| Dilwale | 5.1 |
29. ROUND()
SELECT movie_name,
ROUND(collection_crore / 10, 2) AS collection_in_tens
FROM Movies;
Example output:
| Movie | Collection in tens |
|---|---|
| Jawan | 114.80 |
| Pathaan | 105.00 |
| Chennai Express | 42.30 |
30. String functions
| Function | Example | Result |
|---|---|---|
| UPPER | UPPER('jawan') | JAWAN |
| LOWER | LOWER('PATHAAN') | pathaan |
| LENGTH | LENGTH('Jawan') | 5 |
| CONCAT | CONCAT('SRK',' Movies') | SRK Movies |
| REPLACE | REPLACE('SRK Film','Film','Movie') | SRK Movie |
| SUBSTRING | SUBSTRING('Pathaan',1,4) | Path |
Example:
SELECT
UPPER(movie_name) AS movie_uppercase,
LENGTH(movie_name) AS name_length
FROM Movies;
31. Date functions
Let’s create a movie release date column.
ALTER TABLE Movies
ADD COLUMN release_date DATE;
Examples:
SELECT CURDATE();
SELECT NOW();
SELECT YEAR('2023-09-07');
SELECT MONTH('2023-09-07');
SELECT DAY('2023-09-07');
Output:
CURDATE() Current database date
YEAR('2023-09-07') 2023
MONTH('2023-09-07') 9
DAY('2023-09-07') 7
32. CASE statement
Give movies a rating category.
SELECT
movie_name,
rating,
CASE
WHEN rating >= 7.5 THEN 'Excellent'
WHEN rating >= 6.5 THEN 'Good'
WHEN rating >= 5.5 THEN 'Average'
ELSE 'Below Average'
END AS category
FROM Movies;
Output:
| Movie | Rating | Category |
|---|---|---|
| Jawan | 7.0 | Good |
| Pathaan | 5.8 | Average |
| My Name Is Khan | 7.9 | Excellent |
| Dilwale | 5.1 | Below Average |
PART 5 – GROUP BY and HAVING
33. GROUP BY
Count movies released in each year.
SELECT release_year,
COUNT(*) AS total_movies
FROM Movies
GROUP BY release_year;
Output:
| Year | Total movies |
|---|---|
| 2007 | 1 |
| 2010 | 1 |
| 2013 | 1 |
| 2015 | 1 |
| 2017 | 1 |
| 2023 | 3 |
34. HAVING
Find years having more than one movie.
SELECT release_year,
COUNT(*) AS total_movies
FROM Movies
GROUP BY release_year
HAVING COUNT(*) > 1;
Output:
| Year | Total |
|---|---|
| 2023 | 3 |
Remember:
- WHERE filters individual rows.
- HAVING filters grouped results.
PART 6 – Primary key, foreign key and constraints
35. Create multiple connected tables
A professional movie database should not store every detail in one table.
We will create separate tables for actors, actresses, directors and movies.
Top SQL Projects: Beginners to Advanced with Source Code
Actors table
CREATE TABLE Actors (
actor_id INT PRIMARY KEY,
actor_name VARCHAR(100) NOT NULL
);
INSERT INTO Actors VALUES
(1, 'Shah Rukh Khan'),
(2, 'Salman Khan'),
(3, 'Aamir Khan');
Directors table
CREATE TABLE Directors (
director_id INT PRIMARY KEY,
director_name VARCHAR(100) NOT NULL
);
INSERT INTO Directors VALUES
(1, 'Atlee'),
(2, 'Siddharth Anand'),
(3, 'Rohit Shetty'),
(4, 'Rajkumar Hirani');
Movies table with foreign keys
For this separate relational example, create a new table:
CREATE TABLE Movie_Details (
movie_id INT PRIMARY KEY,
movie_name VARCHAR(100),
actor_id INT,
director_id INT,
release_year INT,
FOREIGN KEY (actor_id)
REFERENCES Actors(actor_id),
FOREIGN KEY (director_id)
REFERENCES Directors(director_id)
);
Insert records:
INSERT INTO Movie_Details VALUES
(1, 'Jawan', 1, 1, 2023),
(2, 'Pathaan', 1, 2, 2023),
(3, 'Chennai Express', 1, 3, 2013),
(4, 'Dunki', 1, 4, 2023);
Output:
4 rows inserted.
36. SQL constraints
| Constraint | Purpose |
|---|---|
| PRIMARY KEY | Unique record identification |
| FOREIGN KEY | Connect tables |
| NOT NULL | Prevent empty values |
| UNIQUE | Prevent duplicate values |
| CHECK | Validate a condition |
| DEFAULT | Set a default value |
| AUTO_INCREMENT | Automatically generate IDs |
Example:
CREATE TABLE Movie_Tickets (
ticket_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100) NOT NULL,
ticket_price DECIMAL(8,2) CHECK(ticket_price > 0),
status VARCHAR(20) DEFAULT 'Booked',
booking_reference VARCHAR(50) UNIQUE
);
PART 7 – SQL JOINS
This is one of the most important topics for interviews and professional database development.
Assume we have:
Actors
| actor_id | actor_name |
|---|---|
| 1 | Shah Rukh Khan |
| 2 | Salman Khan |
| 3 | Aamir Khan |
Movie_Details
| movie_id | movie_name | actor_id |
|---|---|---|
| 1 | Jawan | 1 |
| 2 | Pathaan | 1 |
| 3 | Chennai Express | 1 |
| 4 | Bajrangi Bhaijaan | 2 |
37. INNER JOIN
Only matching records.
SELECT
Actors.actor_name,
Movie_Details.movie_name
FROM Actors
INNER JOIN Movie_Details
ON Actors.actor_id = Movie_Details.actor_id;
Output:
| Actor | Movie |
|---|---|
| Shah Rukh Khan | Jawan |
| Shah Rukh Khan | Pathaan |
| Shah Rukh Khan | Chennai Express |
| Salman Khan | Bajrangi Bhaijaan |
38. LEFT JOIN
All actors, including actors without movies in the second table.
SELECT
Actors.actor_name,
Movie_Details.movie_name
FROM Actors
LEFT JOIN Movie_Details
ON Actors.actor_id = Movie_Details.actor_id;
Output:
| Actor | Movie |
|---|---|
| Shah Rukh Khan | Jawan |
| Shah Rukh Khan | Pathaan |
| Shah Rukh Khan | Chennai Express |
| Salman Khan | Bajrangi Bhaijaan |
| Aamir Khan | NULL |
39. RIGHT JOIN
All movies and matching actors.
SELECT
Actors.actor_name,
Movie_Details.movie_name
FROM Actors
RIGHT JOIN Movie_Details
ON Actors.actor_id = Movie_Details.actor_id;
Output: All four movie records with their matching actors.
40. CROSS JOIN
Every actor combined with every movie.
SELECT
Actors.actor_name,
Movie_Details.movie_name
FROM Actors
CROSS JOIN Movie_Details;
Output:
3 actors × 4 movies = 12 combinations
41. SELF JOIN
Join a table with itself.
SELECT
A.movie_name AS Movie1,
B.movie_name AS Movie2
FROM Movie_Details A
JOIN Movie_Details B
ON A.actor_id = B.actor_id
AND A.movie_id < B.movie_id;
Output:
| Movie1 | Movie2 |
|---|---|
| Jawan | Pathaan |
| Jawan | Chennai Express |
| Pathaan | Chennai Express |
42. UNION and UNION ALL
SELECT movie_name FROM Movie_Details
WHERE release_year = 2023
UNION
SELECT movie_name FROM Movie_Details
WHERE release_year = 2013;
UNION removes duplicate rows.
SELECT movie_name FROM Movie_Details
WHERE release_year = 2023
UNION ALL
SELECT movie_name FROM Movie_Details
WHERE release_year = 2013;
UNION ALL keeps duplicates.
PART 8 – Advanced SQL
43. Subqueries
Find movies with above-average ratings.
SELECT movie_name, rating
FROM Movies
WHERE rating > (
SELECT AVG(rating)
FROM Movies
);
Output:
| Movie | Rating |
|---|---|
| Jawan | 7.0 |
| My Name Is Khan | 7.9 |
| Om Shanti Om | 6.7 |
| Raees | 6.8 |
44. EXISTS
Find actors who have movies.
SELECT actor_name
FROM Actors A
WHERE EXISTS (
SELECT 1
FROM Movie_Details M
WHERE M.actor_id = A.actor_id
);
Output:
Shah Rukh Khan
Salman Khan
45. NOT EXISTS
Find actors without movies.
SELECT actor_name
FROM Actors A
WHERE NOT EXISTS (
SELECT 1
FROM Movie_Details M
WHERE M.actor_id = A.actor_id
);
Output:
Aamir Khan
46. ANY and ALL
Find movies with a collection greater than at least one of the selected comparison values.
SELECT movie_name, collection_crore
FROM Movies
WHERE collection_crore > ANY (
SELECT collection_crore
FROM Movies
WHERE release_year = 2013
);
Find movies with a collection greater than every selected comparison value.
SELECT movie_name, collection_crore
FROM Movies
WHERE collection_crore > ALL (
SELECT collection_crore
FROM Movies
WHERE release_year = 2013
);
47. Common Table Expressions (CTE)
CTEs make complex SQL queries easier to understand.
WITH TopMovies AS (
SELECT movie_name, rating
FROM Movies
WHERE rating >= 7
)
SELECT *
FROM TopMovies;
Output:
| Movie | Rating |
|---|---|
| Jawan | 7.0 |
| My Name Is Khan | 7.9 |
48. Window functions
Window functions perform calculations across related rows without collapsing them into groups.
ROW_NUMBER
SELECT
movie_name,
rating,
ROW_NUMBER() OVER (
ORDER BY rating DESC
) AS movie_rank
FROM Movies;
RANK
SELECT
movie_name,
rating,
RANK() OVER (
ORDER BY rating DESC
) AS movie_rank
FROM Movies;
DENSE_RANK
SELECT
movie_name,
rating,
DENSE_RANK() OVER (
ORDER BY rating DESC
) AS movie_rank
FROM Movies;
Difference:
| Function | Ranking behaviour |
|---|---|
| ROW_NUMBER | Always unique sequential numbers |
| RANK | Ties share rank; next rank is skipped |
| DENSE_RANK | Ties share rank; no skipped rank |
PARTITION BY
SELECT
movie_name,
release_year,
rating,
RANK() OVER (
PARTITION BY release_year
ORDER BY rating DESC
) AS yearly_rank
FROM Movies;
Ranks movies separately within each release year.
PART 9 – Views, indexes and stored procedures
These topics are useful when developing large applications, billing systems, websites and enterprise databases.
49. CREATE VIEW
A view is a virtual table based on a SQL query.
Example: Create a view containing only highly rated movies.
CREATE VIEW Top_Rated_Movies AS
SELECT movie_name, rating, release_year
FROM Movies
WHERE rating >= 7;
Display the view:
SELECT * FROM Top_Rated_Movies;
Output:
| Movie | Rating | Year |
|---|---|---|
| Jawan | 7.0 | 2023 |
| My Name Is Khan | 7.9 | 2010 |
Other commands:
SHOW FULL TABLES
WHERE TABLE_TYPE = 'VIEW';
DROP VIEW Top_Rated_Movies;
50. INDEX
Indexes help MySQL find records faster.
Without an index, MySQL may need to scan many rows.
Create an index:
CREATE INDEX idx_movie_name
ON Movies(movie_name);
Search:
SELECT *
FROM Movies
WHERE movie_name = 'Jawan';
Show indexes:
SHOW INDEX FROM Movies;
Delete an index:
DROP INDEX idx_movie_name
ON Movies;
Types of indexes:
- Single-column index
- Composite index
- Unique index
- Full-text index
- Primary key index
Remember: Indexes improve many read queries but consume storage and can slow down INSERT, UPDATE and DELETE operations.
51. Stored procedures
A stored procedure is a reusable SQL program stored in the database.
Example: Create a procedure to display all movies released in a particular year.
DELIMITER //
CREATE PROCEDURE GetMoviesByYear(
IN selected_year INT
)
BEGIN
SELECT *
FROM Movies
WHERE release_year = selected_year;
END //
DELIMITER ;
Execute:
CALL GetMoviesByYear(2023);
Output:
| Movie | Year |
|---|---|
| Jawan | 2023 |
| Pathaan | 2023 |
| Dunki | 2023 |
Delete procedure:
DROP PROCEDURE GetMoviesByYear;
52. Stored procedure with OUT parameter
Calculate the total number of movies.
DELIMITER //
CREATE PROCEDURE CountMovies(
OUT total INT
)
BEGIN
SELECT COUNT(*)
INTO total
FROM Movies;
END //
DELIMITER ;
Execute:
CALL CountMovies(@total);
SELECT @total AS Total_Movies;
Output:
| Total_Movies |
|---|
| 8 |
53. Triggers
A trigger automatically executes when a specified database event occurs.
For example, whenever a movie is inserted, save the action in an audit table.
Create the audit table:
CREATE TABLE Movie_Audit (
audit_id INT AUTO_INCREMENT PRIMARY KEY,
action_name VARCHAR(50),
movie_name VARCHAR(100),
action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Create the trigger:
DELIMITER //
CREATE TRIGGER After_Movie_Insert
AFTER INSERT ON Movies
FOR EACH ROW
BEGIN
INSERT INTO Movie_Audit(action_name, movie_name)
VALUES ('INSERT', NEW.movie_name);
END //
DELIMITER ;
Now insert:
INSERT INTO Movies
(movie_name, release_year, language,
budget_crore, collection_crore, rating)
VALUES
('Example Movie', 2026, 'Hindi', 100, 200, 7.5);
Check the audit table:
SELECT * FROM Movie_Audit;
Output:
| audit_id | action_name | movie_name |
|---|---|---|
| 1 | INSERT | Example Movie |
NEW refers to the new row. OLD refers to the previous row in applicable UPDATE and DELETE triggers.
54. Transactions
Transactions help maintain database consistency.
Imagine a customer books a movie ticket. You want to deduct money and create a booking. Both operations should succeed together.
START TRANSACTION;
UPDATE Wallets
SET balance = balance - 200
WHERE customer_id = 1;
INSERT INTO Bookings
(customer_id, movie_id, amount)
VALUES (1, 1, 200);
COMMIT;
If something goes wrong:
ROLLBACK;
Important transaction commands:
| Command | Purpose |
|---|---|
| START TRANSACTION | Begin transaction |
| COMMIT | Save changes |
| ROLLBACK | Undo uncommitted changes |
| SAVEPOINT | Create rollback point |
| ROLLBACK TO SAVEPOINT | Return to a savepoint |
Example:
START TRANSACTION;
UPDATE Wallets
SET balance = balance - 100
WHERE customer_id = 1;
SAVEPOINT FirstStep;
UPDATE Wallets
SET balance = balance - 50
WHERE customer_id = 1;
ROLLBACK TO SAVEPOINT FirstStep;
COMMIT;
Only the first deduction is committed.
55. ACID properties
ACID describes the important properties of reliable database transactions.
| Property | Meaning |
|---|---|
| Atomicity | All operations succeed or none do |
| Consistency | Database rules remain valid |
| Isolation | Concurrent transactions are controlled |
| Durability | Committed changes are preserved |
56. Database normalization
Normalization organizes database information to reduce unnecessary duplication and prevent data anomalies.
Imagine this badly designed table:
| Movie | Actor | Actor Phone |
|---|---|---|
| Jawan | SRK | 9999999999 |
| Pathaan | SRK | 9999999999 |
| Dunki | SRK | 9999999999 |
Instead, create two tables.
Actors
| actor_id | actor_name | phone |
|---|---|---|
| 1 | SRK | 9999999999 |
Movies
| movie_id | movie_name | actor_id |
|---|---|---|
| 1 | Jawan | 1 |
| 2 | Pathaan | 1 |
| 3 | Dunki | 1 |
Normalization types:
- 1NF: Each column contains atomic values; no repeating groups.
- 2NF: Meets 1NF and every non-key attribute depends on the entire candidate key.
- 3NF: Meets 2NF and has no transitive dependency of non-key attributes on a candidate key.
- BCNF: Every determinant must be a candidate key.
57. Database security
SQL can control who can access database information.
Create a user:
CREATE USER 'movie_user'@'localhost'
IDENTIFIED BY 'StrongPassword123!';
Grant read-only access:
GRANT SELECT
ON SRK_Movie_DB.*
TO 'movie_user'@'localhost';
Show permissions:
SHOW GRANTS FOR 'movie_user'@'localhost';
Revoke permissions:
REVOKE SELECT
ON SRK_Movie_DB.*
FROM 'movie_user'@'localhost';
Use strong passwords and restrict database permissions to the minimum required.
58. EXPLAIN – Query optimization
EXPLAIN shows how MySQL plans to execute a query.
EXPLAIN
SELECT *
FROM Movies
WHERE movie_name = 'Jawan';
It can show:
- Which table is accessed.
- Which index may be used.
- How many rows may be examined.
- The access method.
For a real application, examine slow queries and use indexes appropriately instead of adding indexes to every column.
PART 10 – Complete movie database design
Now let’s design a more professional movie application.
Our database will have these tables:
| Table | Purpose |
|---|---|
| Movies | Movie details |
| Actors | Actor information |
| Directors | Director information |
| Heroines | Actress information |
| Genres | Movie categories |
| Movie_Actors | Multiple actors per movie |
| Movie_Directors | Director relationships |
| Movie_Genres | Multiple genres per movie |
| Reviews | User movie ratings |
| Users | User information |
| Box_Office | Collection information |
| Awards | Movie awards |
59. Many-to-many relationship
A movie can have multiple actors, and an actor can appear in multiple movies.
This is a many-to-many relationship.
CREATE TABLE Movie_Actors (
movie_id INT,
actor_id INT,
PRIMARY KEY(movie_id, actor_id),
FOREIGN KEY(movie_id)
REFERENCES Movies(movie_id),
FOREIGN KEY(actor_id)
REFERENCES Actors(actor_id)
);
Add actor relationships:
INSERT INTO Movie_Actors VALUES
(1, 1),
(2, 1),
(3, 1);
Find all movies featuring Shah Rukh Khan:
SELECT
M.movie_name,
A.actor_name
FROM Movies M
JOIN Movie_Actors MA
ON M.movie_id = MA.movie_id
JOIN Actors A
ON MA.actor_id = A.actor_id
WHERE A.actor_name = 'Shah Rukh Khan';
60. Advanced challenge: Top 5 movies
SELECT
movie_name,
release_year,
collection_crore,
RANK() OVER (
ORDER BY collection_crore DESC
) AS collection_rank
FROM Movies
LIMIT 5;
61. Advanced challenge: Profit calculation
SELECT
movie_name,
budget_crore,
collection_crore,
(collection_crore - budget_crore) AS difference
FROM Movies
ORDER BY difference DESC;
Note: This is collection minus budget, not actual net profit. Actual profit requires additional revenue and expense information.
62. Advanced challenge: Find movies above average collection by year
WITH YearlyAverage AS (
SELECT
release_year,
AVG(collection_crore) AS avg_collection
FROM Movies
GROUP BY release_year
)
SELECT
M.movie_name,
M.release_year,
M.collection_crore
FROM Movies M
JOIN YearlyAverage Y
ON M.release_year = Y.release_year
WHERE M.collection_crore > Y.avg_collection;
PART 11 – Important SQL interview questions
| No. | Interview question | Short answer |
|---|---|---|
| 1 | What is SQL? | Structured Query Language |
| 2 | What is DBMS? | Database Management System |
| 3 | What is RDBMS? | Relational Database Management System |
| 4 | What is a primary key? | Unique row identifier |
| 5 | What is a foreign key? | Relationship between tables |
| 6 | What is NULL? | Missing or unknown value |
| 7 | DELETE vs TRUNCATE? | Delete removes selected rows; truncate removes all rows |
| 8 | DROP vs TRUNCATE? | DROP removes table; TRUNCATE removes rows |
| 9 | WHERE vs HAVING? | Row filtering vs group filtering |
| 10 | UNION vs UNION ALL? | Duplicate removal vs retaining duplicates |
| 11 | What is a JOIN? | Combines related table records |
| 12 | What is an index? | Helps retrieve records efficiently |
| 13 | What is a view? | Virtual table based on a query |
| 14 | What is a trigger? | Automatically executed database program |
| 15 | What is a stored procedure? | Stored reusable SQL program |
| 16 | What is normalization? | Organizing data to reduce redundancy |
| 17 | What is a transaction? | Logical unit of database operations |
| 18 | What is a subquery? | Query inside another query |
| 19 | What is a CTE? | Named temporary query result |
| 20 | What is a window function? | Calculates across related rows without collapsing them |
PART 12 – More important SQL concepts
63. NULL handling
Find movies without a release date.
SELECT *
FROM Movies
WHERE release_date IS NULL;
Find movies with a release date:
SELECT *
FROM Movies
WHERE release_date IS NOT NULL;
Replace NULL with another value:
SELECT
movie_name,
COALESCE(release_date, '2000-01-01') AS release_date
FROM Movies;
Other useful functions:
SELECT IFNULL(NULL, 'Unknown');
SELECT NULLIF(10, 10);
64. Aliases
Give columns and tables temporary names.
SELECT
movie_name AS Movie,
collection_crore AS Collection
FROM Movies;
Table alias:
SELECT M.movie_name
FROM Movies AS M;
65. Arithmetic operators
| Operator | Meaning |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example:
SELECT
movie_name,
collection_crore - budget_crore AS Difference
FROM Movies;
66. More SQL commands to remember
| Command | Purpose |
|---|---|
| SHOW DATABASES | List databases |
| SHOW TABLES | List tables |
| DESCRIBE Movies | Show table structure |
| SHOW CREATE TABLE Movies | Show table creation SQL |
| USE database_name | Select database |
| RENAME TABLE | Rename table |
| ANALYZE TABLE | Update table statistics |
| OPTIMIZE TABLE | Optimize supported storage structures |
| EXPLAIN | Examine query execution plan |
67. Backup and restore MySQL
Using XAMPP phpMyAdmin:
- Open
http://localhost/phpmyadmin. - Select
SRK_Movie_DB. - Click Export.
- Select SQL format.
- Click Export to download the backup.
To restore:
- Create or select a database.
- Open Import.
- Select your
.sqlfile. - Click Import.
PART 13 – Your practical SQL examination
Try these 20 questions without looking at the earlier examples.
SQL practical exam
0/20 completed
1. Create a database named SRK_Movies.
2. Create a Movies table with ID, name, year and rating.
3. Insert 10 movie records.
4. Display all movie names.
5. Find movies released after 2015.
6. Find movies with a rating above 7.
7. Display movies in descending rating order.
8. Find the highest collection.
9. Find the lowest collection.
10. Count all movies.
11. Calculate average movie rating.
12. Find total collection.
13. Find duplicate release years.
14. Group movies by release year.
15. Find years with more than two movies.
16. Find movies whose names contain ‘Dil’.
17. Create Actors and Directors tables.
18. Join Movies with Directors.
19. Find movies with ratings above the average.
20. Rank movies according to collection using DENSE_RANK.
Reset examination
PART 14 – Practice using your own computer
Since you have XAMPP installed, you can practise these examples directly in phpMyAdmin.
Your SQL practice environment
- Open XAMPP Control Panel.
- Start Apache.
- Start MySQL.
- Open your browser.
- Visit phpMyAdmin.
- Click SQL.
- Paste your SQL query.
- Click Go to see the output.
Recommended learning order
| Stage | What you should achieve |
|---|---|
| Days 1–3 | Database basics and CRUD |
| Days 4–6 | Filtering and functions |
| Days 7–9 | GROUP BY and JOINs |
| Days 10–12 | Subqueries and CTEs |
| Days 13–15 | Window functions |
| Days 16–18 | Views, indexes and procedures |
| Days 19–21 | Transactions, normalization and security |
| Days 22–30 | Complete movie database project |
One important note: The examples use MySQL syntax, suitable for your XAMPP environment. Some SQL features differ between MySQL, PostgreSQL, SQL Server and Oracle.
What I recommend building next
MovieGo–A comprehensive movie management system by Martin Gu on Dribbble
SRK Movie Database – Complete SQL Project
Build a working project with:
- 50+ movie records.
- Actors, actresses and directors.
- Movie ratings and genres.
- Collection reports.
- Top 10 movie rankings.
- Advanced SQL search.
- 100 SQL queries with input and output.
- MySQL database file for phpMyAdmin.
Practical project
You now have the complete SQL learning roadmap, from basic commands through database administration and advanced queries. The best next step is to execute each query yourself and compare its actual output with the examples.
https://www.twincafe.com/ravikanati-sai-charan
SRK_SQL_Complete_Masterclass
say all sql Command
in datil from pdf
sql on bhahubali


