sql twin cafe

By Btech Faqa

Updated On:

Join WhatsApp

Join Now

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

Chennai Express to Pathaan and now Jawan, highest-grossing Shah Rukh Khan films that created new box office records

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_idmovie_nameyearrating
1Jawan20237.0
2Pathaan20235.8
3Chennai Express20136.0
4Dilwale20155.1
5Dunki20236.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

CategoryPurposeCommands
DDLStructure managementCREATE, ALTER, DROP, TRUNCATE
DMLData modificationINSERT, UPDATE, DELETE
DQLData retrievalSELECT
DCLAccess controlGRANT, REVOKE
TCLTransaction managementCOMMIT, 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

IDMovieYearRating
1Jawan20237.0
2Pathaan20235.8
3Chennai Express20136.0
4Dilwale20155.1
5Dunki20236.5
6Raees20176.8
7My Name Is Khan20107.9
8Om Shanti Om20076.7

PART 3 – All basic SQL commands

9. SELECT

Get specific columns.

SELECT movie_name, release_year
FROM Movies;

Output:

movie_namerelease_year
Jawan2023
Pathaan2023
Chennai Express2013
Dilwale2015
Dunki2023
Raees2017
My Name Is Khan2010
Om Shanti Om2007

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:

MovieRating
Jawan7.0
Raees6.8
My Name Is Khan7.9
Om Shanti Om6.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:

PatternMeaning
'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_namerating
My Name Is Khan7.9

28. MIN()

Find the lowest-rated movie.

SELECT movie_name, rating
FROM Movies
WHERE rating = (
    SELECT MIN(rating) FROM Movies
);

Output:

movie_namerating
Dilwale5.1

29. ROUND()

SELECT movie_name,
ROUND(collection_crore / 10, 2) AS collection_in_tens
FROM Movies;

Example output:

MovieCollection in tens
Jawan114.80
Pathaan105.00
Chennai Express42.30

30. String functions

FunctionExampleResult
UPPERUPPER('jawan')JAWAN
LOWERLOWER('PATHAAN')pathaan
LENGTHLENGTH('Jawan')5
CONCATCONCAT('SRK',' Movies')SRK Movies
REPLACEREPLACE('SRK Film','Film','Movie')SRK Movie
SUBSTRINGSUBSTRING('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:

MovieRatingCategory
Jawan7.0Good
Pathaan5.8Average
My Name Is Khan7.9Excellent
Dilwale5.1Below 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:

YearTotal movies
20071
20101
20131
20151
20171
20233

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:

YearTotal
20233

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

bosscoderacademy.com

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

ConstraintPurpose
PRIMARY KEYUnique record identification
FOREIGN KEYConnect tables
NOT NULLPrevent empty values
UNIQUEPrevent duplicate values
CHECKValidate a condition
DEFAULTSet a default value
AUTO_INCREMENTAutomatically 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_idactor_name
1Shah Rukh Khan
2Salman Khan
3Aamir Khan

Movie_Details

movie_idmovie_nameactor_id
1Jawan1
2Pathaan1
3Chennai Express1
4Bajrangi Bhaijaan2

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:

ActorMovie
Shah Rukh KhanJawan
Shah Rukh KhanPathaan
Shah Rukh KhanChennai Express
Salman KhanBajrangi 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:

ActorMovie
Shah Rukh KhanJawan
Shah Rukh KhanPathaan
Shah Rukh KhanChennai Express
Salman KhanBajrangi Bhaijaan
Aamir KhanNULL

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:

Movie1Movie2
JawanPathaan
JawanChennai Express
PathaanChennai 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:

MovieRating
Jawan7.0
My Name Is Khan7.9
Om Shanti Om6.7
Raees6.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:

MovieRating
Jawan7.0
My Name Is Khan7.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:

FunctionRanking behaviour
ROW_NUMBERAlways unique sequential numbers
RANKTies share rank; next rank is skipped
DENSE_RANKTies 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:

MovieRatingYear
Jawan7.02023
My Name Is Khan7.92010

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:

MovieYear
Jawan2023
Pathaan2023
Dunki2023

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_idaction_namemovie_name
1INSERTExample 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:

CommandPurpose
START TRANSACTIONBegin transaction
COMMITSave changes
ROLLBACKUndo uncommitted changes
SAVEPOINTCreate rollback point
ROLLBACK TO SAVEPOINTReturn 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.

PropertyMeaning
AtomicityAll operations succeed or none do
ConsistencyDatabase rules remain valid
IsolationConcurrent transactions are controlled
DurabilityCommitted changes are preserved

56. Database normalization

Normalization organizes database information to reduce unnecessary duplication and prevent data anomalies.

Imagine this badly designed table:

MovieActorActor Phone
JawanSRK9999999999
PathaanSRK9999999999
DunkiSRK9999999999

Instead, create two tables.

Actors

actor_idactor_namephone
1SRK9999999999

Movies

movie_idmovie_nameactor_id
1Jawan1
2Pathaan1
3Dunki1

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.

cs.sfu.ca

Our database will have these tables:

TablePurpose
MoviesMovie details
ActorsActor information
DirectorsDirector information
HeroinesActress information
GenresMovie categories
Movie_ActorsMultiple actors per movie
Movie_DirectorsDirector relationships
Movie_GenresMultiple genres per movie
ReviewsUser movie ratings
UsersUser information
Box_OfficeCollection information
AwardsMovie 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 questionShort answer
1What is SQL?Structured Query Language
2What is DBMS?Database Management System
3What is RDBMS?Relational Database Management System
4What is a primary key?Unique row identifier
5What is a foreign key?Relationship between tables
6What is NULL?Missing or unknown value
7DELETE vs TRUNCATE?Delete removes selected rows; truncate removes all rows
8DROP vs TRUNCATE?DROP removes table; TRUNCATE removes rows
9WHERE vs HAVING?Row filtering vs group filtering
10UNION vs UNION ALL?Duplicate removal vs retaining duplicates
11What is a JOIN?Combines related table records
12What is an index?Helps retrieve records efficiently
13What is a view?Virtual table based on a query
14What is a trigger?Automatically executed database program
15What is a stored procedure?Stored reusable SQL program
16What is normalization?Organizing data to reduce redundancy
17What is a transaction?Logical unit of database operations
18What is a subquery?Query inside another query
19What is a CTE?Named temporary query result
20What 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

OperatorMeaning
+Addition
–Subtraction
*Multiplication
/Division
%Modulus

Example:

SELECT
movie_name,
collection_crore - budget_crore AS Difference
FROM Movies;

66. More SQL commands to remember

CommandPurpose
SHOW DATABASESList databases
SHOW TABLESList tables
DESCRIBE MoviesShow table structure
SHOW CREATE TABLE MoviesShow table creation SQL
USE database_nameSelect database
RENAME TABLERename table
ANALYZE TABLEUpdate table statistics
OPTIMIZE TABLEOptimize supported storage structures
EXPLAINExamine query execution plan

67. Backup and restore MySQL

Using XAMPP phpMyAdmin:

  1. Open http://localhost/phpmyadmin.
  2. Select SRK_Movie_DB.
  3. Click Export.
  4. Select SQL format.
  5. Click Export to download the backup.

To restore:

  1. Create or select a database.
  2. Open Import.
  3. Select your .sql file.
  4. 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

  1. Open XAMPP Control Panel.
  2. Start Apache.
  3. Start MySQL.
  4. Open your browser.
  5. Visit phpMyAdmin.
  6. Click SQL.
  7. Paste your SQL query.
  8. Click Go to see the output.

Recommended learning order

StageWhat you should achieve
Days 1–3Database basics and CRUD
Days 4–6Filtering and functions
Days 7–9GROUP BY and JOINs
Days 10–12Subqueries and CTEs
Days 13–15Window functions
Days 16–18Views, indexes and procedures
Days 19–21Transactions, normalization and security
Days 22–30Complete 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

dribbble.com

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

🔴Related Post

Leave a Comment