SQL Interview Questions !!
๐ Write a query to find all employees whose salaries exceed the company's average salary.
๐ Write a query to retrieve the names of employees who work in the same department as 'John Doe'.
๐ Write a query to display the second highest salary from the Employee table without using the MAX function twice.
๐ Write a query to find all customers who have placed more than five orders.
๐ Write a query to count the total number of orders placed by each customer.
๐ Write a query to list employees who joined the company within the last 6 months.
๐ Write a query to calculate the total sales amount for each product.
๐ Write a query to list all products that have never been sold.
๐ Write a query to remove duplicate rows from a table.
๐ Write a query to identify the top 10 customers who have not placed any orders in the past year.
Here you can find essential SQL Interview Resources๐
https://t.me/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
๐ Write a query to find all employees whose salaries exceed the company's average salary.
๐ Write a query to retrieve the names of employees who work in the same department as 'John Doe'.
๐ Write a query to display the second highest salary from the Employee table without using the MAX function twice.
๐ Write a query to find all customers who have placed more than five orders.
๐ Write a query to count the total number of orders placed by each customer.
๐ Write a query to list employees who joined the company within the last 6 months.
๐ Write a query to calculate the total sales amount for each product.
๐ Write a query to list all products that have never been sold.
๐ Write a query to remove duplicate rows from a table.
๐ Write a query to identify the top 10 customers who have not placed any orders in the past year.
Here you can find essential SQL Interview Resources๐
https://t.me/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค5๐1
๐ ๐ง๐ผ๐ฝ ๐ฐ ๐๐ฅ๐๐ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป๐ ๐ง๐ผ ๐๐ฒ๐ฎ๐ฟ๐ป ๐๐ฎ๐๐ฎ ๐ถ๐ป ๐ฎ๐ฌ๐ฎ๐ฒ ๐
Want to become a Data Analyst or Data Scientist? ๐
These FREE certifications can help you build job-ready skills & strengthen your resume ๐ฅ
โจ Learn:
โ SQL & Data Analytics
โ Power BI Dashboards ๐
โ Data Cleaning & Visualization
โ AI & Machine Learning Basics ๐ค
๐ฏ FREE + Beginner Friendly
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4dsdTCV
๐ Perfect for Students, Freshers & Career Switchers
Want to become a Data Analyst or Data Scientist? ๐
These FREE certifications can help you build job-ready skills & strengthen your resume ๐ฅ
โจ Learn:
โ SQL & Data Analytics
โ Power BI Dashboards ๐
โ Data Cleaning & Visualization
โ AI & Machine Learning Basics ๐ค
๐ฏ FREE + Beginner Friendly
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4dsdTCV
๐ Perfect for Students, Freshers & Career Switchers
โค1
๐๏ธ SQL Developer Roadmap
๐ SQL Basics (SELECT, WHERE, ORDER BY)
โ๐ Joins (INNER, LEFT, RIGHT, FULL)
โ๐ Aggregate Functions (COUNT, SUM, AVG)
โ๐ Grouping Data (GROUP BY, HAVING)
โ๐ Subqueries & Nested Queries
โ๐ Data Modification (INSERT, UPDATE, DELETE)
โ๐ Database Design (Normalization, Keys)
โ๐ Indexing & Query Optimization
โ๐ Stored Procedures & Functions
โ๐ Transactions & Locks
โ๐ Views & Triggers
โ๐ Backup & Restore
โ๐ Working with NoSQL basics (optional)
โ๐ Real Projects & Practice
โโ Apply for SQL Dev Roles
โค๏ธ React for More!
๐ SQL Basics (SELECT, WHERE, ORDER BY)
โ๐ Joins (INNER, LEFT, RIGHT, FULL)
โ๐ Aggregate Functions (COUNT, SUM, AVG)
โ๐ Grouping Data (GROUP BY, HAVING)
โ๐ Subqueries & Nested Queries
โ๐ Data Modification (INSERT, UPDATE, DELETE)
โ๐ Database Design (Normalization, Keys)
โ๐ Indexing & Query Optimization
โ๐ Stored Procedures & Functions
โ๐ Transactions & Locks
โ๐ Views & Triggers
โ๐ Backup & Restore
โ๐ Working with NoSQL basics (optional)
โ๐ Real Projects & Practice
โโ Apply for SQL Dev Roles
โค๏ธ React for More!
โค14๐1
Want to start your career in ๐๐ & ๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ๐?
Learn from IIIT Bangalore & upGrad
๐ซ Beginner Friendly
๐ซ Industry Recognized Certificate
๐ซHigh Demand Career Skills
๐๐ผ๐ผ๐ธ ๐๐ฅ๐๐ ๐๐ผ๐๐ป๐๐ฒ๐น๐น๐ถ๐ป๐ด๐Now & explore your career roadmap
https://pdlink.in/4twH9xg
๐Top roles you can target:
* Data Analyst , AI Engineer ,Machine Learning Engineer & Data Scientist
Learn from IIIT Bangalore & upGrad
๐ซ Beginner Friendly
๐ซ Industry Recognized Certificate
๐ซHigh Demand Career Skills
๐๐ผ๐ผ๐ธ ๐๐ฅ๐๐ ๐๐ผ๐๐ป๐๐ฒ๐น๐น๐ถ๐ป๐ด๐Now & explore your career roadmap
https://pdlink.in/4twH9xg
๐Top roles you can target:
* Data Analyst , AI Engineer ,Machine Learning Engineer & Data Scientist
โค2๐คฃ1
SQL interview questions with answers ๐๐
1. Question: What is SQL?
Answer: SQL (Structured Query Language) is a programming language designed for managing and manipulating relational databases. It is used to query, insert, update, and delete data in databases.
2. Question: Differentiate between SQL and MySQL.
Answer: SQL is a language for managing relational databases, while MySQL is an open-source relational database management system (RDBMS) that uses SQL as its language.
3. Question: Explain the difference between INNER JOIN and LEFT JOIN.
Answer: INNER JOIN returns rows when there is a match in both tables, while LEFT JOIN returns all rows from the left table and the matched rows from the right table, filling in with NULLs for non-matching rows.
4. Question: How do you remove duplicate records from a table?
Answer: Use the
5. Question: What is a subquery in SQL?
Answer: A subquery is a query nested inside another query. It can be used to retrieve data that will be used in the main query as a condition to further restrict the data to be retrieved.
6. Question: Explain the purpose of the GROUP BY clause.
Answer: The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows, like when using aggregate functions such as COUNT, SUM, AVG, etc.
7. Question: How can you add a new record to a table?
Answer: Use the
8. Question: What is the purpose of the HAVING clause?
Answer: The HAVING clause is used in combination with the GROUP BY clause to filter the results of aggregate functions based on a specified condition.
9. Question: Explain the concept of normalization in databases.
Answer: Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves breaking down tables into smaller, related tables.
10. Question: How do you update data in a table in SQL?
Answer: Use the
Here is an amazing resources to learn & practice SQL: https://bit.ly/3FxxKPz
Share with credits: https://t.me/sqlspecialist
Hope it helps :)
1. Question: What is SQL?
Answer: SQL (Structured Query Language) is a programming language designed for managing and manipulating relational databases. It is used to query, insert, update, and delete data in databases.
2. Question: Differentiate between SQL and MySQL.
Answer: SQL is a language for managing relational databases, while MySQL is an open-source relational database management system (RDBMS) that uses SQL as its language.
3. Question: Explain the difference between INNER JOIN and LEFT JOIN.
Answer: INNER JOIN returns rows when there is a match in both tables, while LEFT JOIN returns all rows from the left table and the matched rows from the right table, filling in with NULLs for non-matching rows.
4. Question: How do you remove duplicate records from a table?
Answer: Use the
DISTINCT keyword in a SELECT statement to retrieve unique records. For example: SELECT DISTINCT column1, column2 FROM table;5. Question: What is a subquery in SQL?
Answer: A subquery is a query nested inside another query. It can be used to retrieve data that will be used in the main query as a condition to further restrict the data to be retrieved.
6. Question: Explain the purpose of the GROUP BY clause.
Answer: The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows, like when using aggregate functions such as COUNT, SUM, AVG, etc.
7. Question: How can you add a new record to a table?
Answer: Use the
INSERT INTO statement. For example: INSERT INTO table_name (column1, column2) VALUES (value1, value2);8. Question: What is the purpose of the HAVING clause?
Answer: The HAVING clause is used in combination with the GROUP BY clause to filter the results of aggregate functions based on a specified condition.
9. Question: Explain the concept of normalization in databases.
Answer: Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves breaking down tables into smaller, related tables.
10. Question: How do you update data in a table in SQL?
Answer: Use the
UPDATE statement to modify existing records in a table. For example: UPDATE table_name SET column1 = value1 WHERE condition;Here is an amazing resources to learn & practice SQL: https://bit.ly/3FxxKPz
Share with credits: https://t.me/sqlspecialist
Hope it helps :)
โค9
๐๏ธ ๐ง๐ผ๐ฝ ๐ฑ ๐๐ฅ๐๐ ๐ฆ๐ค๐ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐
SQL is one of the most important skills for Data Analyst & Tech jobs in 2026 ๐ฅ
These FREE certification courses can help you learn SQL from scratch & boost your resume ๐ผ
โจ Learn:
โ SQL Queries & Databases ๐๏ธ
โ Data Analysis Basics ๐
โ Real-world Projects
โ Beginner to Advanced Concepts
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4dCHiKI
๐ฏ Beginner Friendly + FREE Certificates ๐
๐ผ Perfect for Students, Freshers & Career Switchers
SQL is one of the most important skills for Data Analyst & Tech jobs in 2026 ๐ฅ
These FREE certification courses can help you learn SQL from scratch & boost your resume ๐ผ
โจ Learn:
โ SQL Queries & Databases ๐๏ธ
โ Data Analysis Basics ๐
โ Real-world Projects
โ Beginner to Advanced Concepts
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4dCHiKI
๐ฏ Beginner Friendly + FREE Certificates ๐
๐ผ Perfect for Students, Freshers & Career Switchers
โ
Useful SQL Concepts You Should Know ๐๐
1๏ธโฃ Constraints in SQL:
- PRIMARY KEY โ Uniquely identifies each row
- FOREIGN KEY โ Links to another table
- UNIQUE โ Ensures all values are different
- NOT NULL โ Column must have a value
- CHECK โ Validates data before insert/update
2๏ธโฃ SQL Views:
Virtual tables based on result of a query
CREATE VIEW top_students AS
SELECT name, marks FROM students WHERE marks > 90;
3๏ธโฃ Indexing:
Improves query performance
CREATE INDEX idx_name ON employees(name);
4๏ธโฃ SQL Transactions:
Ensure data integrity
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
5๏ธโฃ Triggers:
Automatic actions when events occur
CREATE TRIGGER log_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO logs(action) VALUES ('Employee updated');
6๏ธโฃ Stored Procedures:
Reusable blocks of SQL logic
CREATE PROCEDURE getTopStudents()
BEGIN
SELECT FROM students WHERE marks > 90;
END;
7๏ธโฃ Common Table Expressions (CTEs):
Temporary named result sets
WITH dept_count AS (
SELECT department, COUNT() AS total FROM employees GROUP BY department
)
SELECT FROM dept_count;
๐ฌ Double Tap โค๏ธ For More!
1๏ธโฃ Constraints in SQL:
- PRIMARY KEY โ Uniquely identifies each row
- FOREIGN KEY โ Links to another table
- UNIQUE โ Ensures all values are different
- NOT NULL โ Column must have a value
- CHECK โ Validates data before insert/update
2๏ธโฃ SQL Views:
Virtual tables based on result of a query
CREATE VIEW top_students AS
SELECT name, marks FROM students WHERE marks > 90;
3๏ธโฃ Indexing:
Improves query performance
CREATE INDEX idx_name ON employees(name);
4๏ธโฃ SQL Transactions:
Ensure data integrity
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
5๏ธโฃ Triggers:
Automatic actions when events occur
CREATE TRIGGER log_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO logs(action) VALUES ('Employee updated');
6๏ธโฃ Stored Procedures:
Reusable blocks of SQL logic
CREATE PROCEDURE getTopStudents()
BEGIN
SELECT FROM students WHERE marks > 90;
END;
7๏ธโฃ Common Table Expressions (CTEs):
Temporary named result sets
WITH dept_count AS (
SELECT department, COUNT() AS total FROM employees GROUP BY department
)
SELECT FROM dept_count;
๐ฌ Double Tap โค๏ธ For More!
โค5
SQL Interview Questions with Answers Part-1: โ๏ธ
1. What is SQL?
SQL (Structured Query Language) is a standardized programming language designed to manage and manipulate relational databases. It allows you to query, insert, update, and delete data, as well as create and modify schema objects like tables and views.
2. Differentiate between SQL and NoSQL databases.
SQL databases are relational, table-based, and use structured query language with fixed schemas, ideal for complex queries and transactions. NoSQL databases are non-relational, can be document, key-value, graph, or column-oriented, and are schema-flexible, designed for scalability and handling unstructured data.
3. What are the different types of SQL commands?
โฆ DDL (Data Definition Language): CREATE, ALTER, DROP (define and modify structure)
โฆ DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE (data operations)
โฆ DCL (Data Control Language): GRANT, REVOKE (permission control)
โฆ TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT (transaction management)
4. Explain the difference between WHERE and HAVING clauses.
โฆ
โฆ
5. Write a SQL query to find the second highest salary in a table.
Using a subquery:
Or using DENSE_RANK():
6. What is a JOIN? Explain different types of JOINs.
A JOIN combines rows from two or more tables based on a related column:
โฆ INNER JOIN: returns matching rows from both tables.
โฆ LEFT JOIN (LEFT OUTER JOIN): all rows from the left table, matched rows from right.
โฆ RIGHT JOIN (RIGHT OUTER JOIN): all rows from right table, matched rows from left.
โฆ FULL JOIN (FULL OUTER JOIN): all rows when thereโs a match in either table.
โฆ CROSS JOIN: Cartesian product of both tables.
7. How do you optimize slow-performing SQL queries?
โฆ Use indexes appropriately to speed up lookups.
โฆ Avoid SELECT *; only select necessary columns.
โฆ Use joins carefully; filter early with WHERE clauses.
โฆ Analyze execution plans to identify bottlenecks.
โฆ Avoid unnecessary subqueries; use EXISTS or JOINs.
โฆ Limit result sets with pagination if dealing with large datasets.
8. What is a primary key? What is a foreign key?
โฆ Primary Key: A unique identifier for records in a table; it cannot be NULL.
โฆ Foreign Key: A field that creates a link between two tables by referring to the primary key in another table, enforcing referential integrity.
9. What are indexes? Explain clustered and non-clustered indexes.
โฆ Indexes speed up data retrieval by providing quick lookups.
โฆ Clustered Index: Sorts and stores the actual data rows in the table based on the key; a table can have only one clustered index.
โฆ Non-Clustered Index: Creates a separate structure that points to the data rows; tables can have multiple non-clustered indexes.
10. Write a SQL query to fetch the top 5 records from a table.
In SQL Server and PostgreSQL:
In SQL Server (older syntax):
React โฅ๏ธ for Part 2
1. What is SQL?
SQL (Structured Query Language) is a standardized programming language designed to manage and manipulate relational databases. It allows you to query, insert, update, and delete data, as well as create and modify schema objects like tables and views.
2. Differentiate between SQL and NoSQL databases.
SQL databases are relational, table-based, and use structured query language with fixed schemas, ideal for complex queries and transactions. NoSQL databases are non-relational, can be document, key-value, graph, or column-oriented, and are schema-flexible, designed for scalability and handling unstructured data.
3. What are the different types of SQL commands?
โฆ DDL (Data Definition Language): CREATE, ALTER, DROP (define and modify structure)
โฆ DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE (data operations)
โฆ DCL (Data Control Language): GRANT, REVOKE (permission control)
โฆ TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT (transaction management)
4. Explain the difference between WHERE and HAVING clauses.
โฆ
WHERE filters rows before grouping (used with SELECT, UPDATE).โฆ
HAVING filters groups after aggregation (used with GROUP BY), e.g., filtering aggregated results like sums or counts.5. Write a SQL query to find the second highest salary in a table.
Using a subquery:
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Or using DENSE_RANK():
SELECT salary FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rnk
FROM employees) t
WHERE rnk = 2;
6. What is a JOIN? Explain different types of JOINs.
A JOIN combines rows from two or more tables based on a related column:
โฆ INNER JOIN: returns matching rows from both tables.
โฆ LEFT JOIN (LEFT OUTER JOIN): all rows from the left table, matched rows from right.
โฆ RIGHT JOIN (RIGHT OUTER JOIN): all rows from right table, matched rows from left.
โฆ FULL JOIN (FULL OUTER JOIN): all rows when thereโs a match in either table.
โฆ CROSS JOIN: Cartesian product of both tables.
7. How do you optimize slow-performing SQL queries?
โฆ Use indexes appropriately to speed up lookups.
โฆ Avoid SELECT *; only select necessary columns.
โฆ Use joins carefully; filter early with WHERE clauses.
โฆ Analyze execution plans to identify bottlenecks.
โฆ Avoid unnecessary subqueries; use EXISTS or JOINs.
โฆ Limit result sets with pagination if dealing with large datasets.
8. What is a primary key? What is a foreign key?
โฆ Primary Key: A unique identifier for records in a table; it cannot be NULL.
โฆ Foreign Key: A field that creates a link between two tables by referring to the primary key in another table, enforcing referential integrity.
9. What are indexes? Explain clustered and non-clustered indexes.
โฆ Indexes speed up data retrieval by providing quick lookups.
โฆ Clustered Index: Sorts and stores the actual data rows in the table based on the key; a table can have only one clustered index.
โฆ Non-Clustered Index: Creates a separate structure that points to the data rows; tables can have multiple non-clustered indexes.
10. Write a SQL query to fetch the top 5 records from a table.
In SQL Server and PostgreSQL:
SELECT * FROM table_name
ORDER BY some_column DESC
LIMIT 5;
In SQL Server (older syntax):
SELECT TOP 5 * FROM table_name
ORDER BY some_column DESC;
React โฅ๏ธ for Part 2
โค14
๐๐ ๐ฎ๐ป๐ฑ ๐ ๐ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ ๐ฏ๐ ๐๐๐, ๐๐๐ง ๐ ๐ฎ๐ป๐ฑ๐ถ๐
Freshers get 15 LPA Average Salary with AI & ML Skills!
๐ป 100% Online
โณ 6 Months Duration
๐จโ๐ซ Learn from IIT Professors
๐ Open for Students ,Freshers & Working Professionals
๐ผ Placement Assistance with 5000+ Companies
๐ High Demand Skills for Future Tech Jobs
Top companies are hiring for candidates with ๐๐, ๐ ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด skills in 2026
๐ฅDeadline :- 17th May
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4nmI024
.
Get Placement Assistance With 5000+ Companies
Freshers get 15 LPA Average Salary with AI & ML Skills!
๐ป 100% Online
โณ 6 Months Duration
๐จโ๐ซ Learn from IIT Professors
๐ Open for Students ,Freshers & Working Professionals
๐ผ Placement Assistance with 5000+ Companies
๐ High Demand Skills for Future Tech Jobs
Top companies are hiring for candidates with ๐๐, ๐ ๐ฎ๐ฐ๐ต๐ถ๐ป๐ฒ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด skills in 2026
๐ฅDeadline :- 17th May
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4nmI024
.
Get Placement Assistance With 5000+ Companies
โค1
โ
Stored Procedures ๐ฏ
๐ง 1. What is a Stored Procedure?
A Stored Procedure is a saved SQL program
โข Stored inside the database
โข Can be executed anytime
Think like this ๐
โReusable SQL code blockโ
โก 2. Why Use Stored Procedures?
โข Reuse SQL logic
โข Reduce repeated code
โข Better security
โข Faster execution for repeated tasks
โก 3. Basic Syntax
๐ MySQL Example:
DELIMITER //
CREATE PROCEDURE GetEmployees()
BEGIN
SELECT * FROM employees;
END //
DELIMITER ;
โถ๏ธ 4. Execute Stored Procedure
CALL GetEmployees();
๐ฅ 5. Procedure with Parameter
DELIMITER //
CREATE PROCEDURE GetDeptEmployees(IN dept_name VARCHAR(50))
BEGIN
SELECT * FROM employees
WHERE department = dept_name;
END //
DELIMITER ;
โถ๏ธ 6. Execute Parameterized Procedure
CALL GetDeptEmployees('IT');
โ 7. Drop Stored Procedure
DROP PROCEDURE GetEmployees;
๐ฏ 8. Real Example
Increase salary for all IT employees:
DELIMITER //
CREATE PROCEDURE IncreaseSalary()
BEGIN
UPDATE employees
SET salary = salary + 5000
WHERE department = 'IT';
END //
DELIMITER ;
๐ฏ 9. Practice Tasks
1. Create procedure to show all employees
2. Create procedure for HR employees
3. Create procedure with salary parameter
4. Execute stored procedure
5. Drop procedure
โก Mini Challenge ๐ฅ
Create procedure to return employees with salary > given value
โ Solution
DELIMITER //
CREATE PROCEDURE GetHighSalaryEmployees(IN min_salary INT)
BEGIN
SELECT *
FROM employees
WHERE salary > min_salary;
END //
DELIMITER ;
โถ๏ธ Execute Procedure
CALL GetHighSalaryEmployees(50000);
โ Returns employees earning more than 50k
Key Difference:
โข Function โ returns value
โข Procedure โ performs action / multiple operations
Double Tap โค๏ธ For More
๐ง 1. What is a Stored Procedure?
A Stored Procedure is a saved SQL program
โข Stored inside the database
โข Can be executed anytime
Think like this ๐
โReusable SQL code blockโ
โก 2. Why Use Stored Procedures?
โข Reuse SQL logic
โข Reduce repeated code
โข Better security
โข Faster execution for repeated tasks
โก 3. Basic Syntax
๐ MySQL Example:
DELIMITER //
CREATE PROCEDURE GetEmployees()
BEGIN
SELECT * FROM employees;
END //
DELIMITER ;
โถ๏ธ 4. Execute Stored Procedure
CALL GetEmployees();
๐ฅ 5. Procedure with Parameter
DELIMITER //
CREATE PROCEDURE GetDeptEmployees(IN dept_name VARCHAR(50))
BEGIN
SELECT * FROM employees
WHERE department = dept_name;
END //
DELIMITER ;
โถ๏ธ 6. Execute Parameterized Procedure
CALL GetDeptEmployees('IT');
โ 7. Drop Stored Procedure
DROP PROCEDURE GetEmployees;
๐ฏ 8. Real Example
Increase salary for all IT employees:
DELIMITER //
CREATE PROCEDURE IncreaseSalary()
BEGIN
UPDATE employees
SET salary = salary + 5000
WHERE department = 'IT';
END //
DELIMITER ;
๐ฏ 9. Practice Tasks
1. Create procedure to show all employees
2. Create procedure for HR employees
3. Create procedure with salary parameter
4. Execute stored procedure
5. Drop procedure
โก Mini Challenge ๐ฅ
Create procedure to return employees with salary > given value
โ Solution
DELIMITER //
CREATE PROCEDURE GetHighSalaryEmployees(IN min_salary INT)
BEGIN
SELECT *
FROM employees
WHERE salary > min_salary;
END //
DELIMITER ;
โถ๏ธ Execute Procedure
CALL GetHighSalaryEmployees(50000);
โ Returns employees earning more than 50k
Key Difference:
โข Function โ returns value
โข Procedure โ performs action / multiple operations
Double Tap โค๏ธ For More
โค3
๐ ๐๐ฒ๐ฐ๐ผ๐บ๐ฒ ๐๐ผ๐ฏ-๐ฅ๐ฒ๐ฎ๐ฑ๐ ๐ถ๐ป ๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ & ๐๐ ๐๐ถ๐๐ต ๐๐ป๐ฑ๐๐๐๐ฟ๐ ๐๐
๐ฝ๐ฒ๐ฟ๐๐! ๐
Learn the most in-demand skills of 2026
๐ซData Science ,AI,ML &Python & SQL
โ
๐ผ Get Placement Assistance
๐ Beginner Friendly Program
๐ป Learn Online from Anywhere
๐ Build Skills Companies Actually Hire For
๐ฅ AI is changing every industry โ this is the best time to upskill and secure high-paying tech jobs.
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ ๐๐จ๐ฐ ๐:-
https://pdlink.in/4fdWxJB
โก Limited Seats Available โ Apply Fast!
Learn the most in-demand skills of 2026
๐ซData Science ,AI,ML &Python & SQL
โ
๐ผ Get Placement Assistance
๐ Beginner Friendly Program
๐ป Learn Online from Anywhere
๐ Build Skills Companies Actually Hire For
๐ฅ AI is changing every industry โ this is the best time to upskill and secure high-paying tech jobs.
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ ๐๐จ๐ฐ ๐:-
https://pdlink.in/4fdWxJB
โก Limited Seats Available โ Apply Fast!
โค1
What is a Stored Procedure?
Anonymous Quiz
6%
A. A table
83%
B. A saved SQL program stored in database
7%
C. A database index
4%
D. A view
Which command is used to execute a stored procedure in MySQL?
Anonymous Quiz
54%
A. EXECUTE
20%
B. RUN
22%
C. CALL
4%
D. START
Which keyword is used to pass input parameter in MySQL procedure?
Anonymous Quiz
4%
A. OUT
38%
B. INPUT
28%
C. PARAM
31%
D. IN
What is the advantage of stored procedures?
Anonymous Quiz
10%
A. Reduces code repetition
10%
B. Improves reusability
33%
C. Better performance for repeated execution
47%
D. All of the above
๐ฃ๐ฟ๐ผ๐ฑ๐๐ฐ๐ ๐ ๐ฎ๐ป๐ฎ๐ด๐ฒ๐บ๐ฒ๐ป๐ ๐๐ถ๐๐ต ๐๐ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ by iHUB IIT Roorkee ๐
Freshers get paid 12 LPA average salary for the role of Associate Product Manager! ๐ผ
๐๐ถ๐ด๐ต๐น๐ถ๐ด๐ต๐๐:
โ Learn from IIT Roorkee Professors
โ Placement support from 5,000+ companies
โ Professional Certification in Product Management with Applied AI
โ 100% Online Program
โ Open to Everyone
๐ ๐๐ฒ๐ฎ๐ฑ๐น๐ถ๐ป๐ฒ: 17th May 2026
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4ddJZ5C
โก Limited Seats Available โ Apply Soon!
Freshers get paid 12 LPA average salary for the role of Associate Product Manager! ๐ผ
๐๐ถ๐ด๐ต๐น๐ถ๐ด๐ต๐๐:
โ Learn from IIT Roorkee Professors
โ Placement support from 5,000+ companies
โ Professional Certification in Product Management with Applied AI
โ 100% Online Program
โ Open to Everyone
๐ ๐๐ฒ๐ฎ๐ฑ๐น๐ถ๐ป๐ฒ: 17th May 2026
๐๐ฝ๐ฝ๐น๐ ๐ก๐ผ๐๐ :-
https://pdlink.in/4ddJZ5C
โก Limited Seats Available โ Apply Soon!
๐ฅ Now, letโs move to the next topic:
Triggers in SQL
(Automation inside database ๐ฏ)
๐ง 1. What is a Trigger?
A Trigger is a special SQL block
๐ that runs automatically
๐ when an event happens in a table
Think like this ๐
๐ โAutomatic action on INSERT / UPDATE / DELETEโ
โก 2. Why Use Triggers?
โ Automatic logging
โ Data validation
โ Audit tracking
โ Prevent invalid operations
โก 3. Types of Triggers
BEFORE INSERT โ Runs before inserting data
AFTER INSERT โ Runs after inserting data
BEFORE UPDATE โ Runs before updating
AFTER UPDATE โ Runs after updating
BEFORE DELETE โ Runs before deleting
AFTER DELETE โ Runs after deleting
๐ฅ 4. Basic Trigger Example
๐ Automatically log inserted employee
CREATE TABLE employee_log (
log_message VARCHAR(255)
);
DELIMITER //
CREATE TRIGGER after_employee_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_log
VALUES (CONCAT('New employee added: ', NEW.name));
END //
DELIMITER ;
๐ง 5. Important Keywords
NEW โ New inserted/updated value
OLD โ Previous value before update/delete
โก 6. BEFORE UPDATE Example
๐ Prevent negative salary
DELIMITER //
CREATE TRIGGER check_salary
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be negative';
END IF;
END //
DELIMITER ;
โ 7. Drop Trigger
DROP TRIGGER after_employee_insert;
๐ฏ 8. Practice Tasks
1. Create AFTER INSERT trigger
2. Create BEFORE UPDATE trigger
3. Prevent negative salary using trigger
4. Log deleted employees
5. Drop created trigger
โก Mini Challenge ๐ฅ
๐ Create trigger to automatically save deleted employee names into another table
๐ฅ Mini Challenge Solution
๐ Automatically save deleted employee names into another table
โ Step 1: Create Log Table
CREATE TABLE deleted_employees (
emp_id INT,
name VARCHAR(50),
deleted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
โ Step 2: Create Trigger
DELIMITER //
CREATE TRIGGER log_deleted_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO deleted_employees(emp_id, name)
VALUES (OLD.emp_id, OLD.name);
END //
DELIMITER ;
๐ง How It Works
๐ AFTER DELETE โ runs automatically after deletion
๐ OLD.emp_id and OLD.name
Access deleted row values before they disappear
โ Example
DELETE FROM employees
WHERE emp_id = 101;
โ Deleted employee info automatically saved in deleted_employees table ๐ฏ
๐ฅ Pro Tip
Triggers are powerful but:
โ Too many triggers can slow database
โ Use them carefully ๐ฏ
Double Tap โค๏ธ For More
Triggers in SQL
(Automation inside database ๐ฏ)
๐ง 1. What is a Trigger?
A Trigger is a special SQL block
๐ that runs automatically
๐ when an event happens in a table
Think like this ๐
๐ โAutomatic action on INSERT / UPDATE / DELETEโ
โก 2. Why Use Triggers?
โ Automatic logging
โ Data validation
โ Audit tracking
โ Prevent invalid operations
โก 3. Types of Triggers
BEFORE INSERT โ Runs before inserting data
AFTER INSERT โ Runs after inserting data
BEFORE UPDATE โ Runs before updating
AFTER UPDATE โ Runs after updating
BEFORE DELETE โ Runs before deleting
AFTER DELETE โ Runs after deleting
๐ฅ 4. Basic Trigger Example
๐ Automatically log inserted employee
CREATE TABLE employee_log (
log_message VARCHAR(255)
);
DELIMITER //
CREATE TRIGGER after_employee_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_log
VALUES (CONCAT('New employee added: ', NEW.name));
END //
DELIMITER ;
๐ง 5. Important Keywords
NEW โ New inserted/updated value
OLD โ Previous value before update/delete
โก 6. BEFORE UPDATE Example
๐ Prevent negative salary
DELIMITER //
CREATE TRIGGER check_salary
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be negative';
END IF;
END //
DELIMITER ;
โ 7. Drop Trigger
DROP TRIGGER after_employee_insert;
๐ฏ 8. Practice Tasks
1. Create AFTER INSERT trigger
2. Create BEFORE UPDATE trigger
3. Prevent negative salary using trigger
4. Log deleted employees
5. Drop created trigger
โก Mini Challenge ๐ฅ
๐ Create trigger to automatically save deleted employee names into another table
๐ฅ Mini Challenge Solution
๐ Automatically save deleted employee names into another table
โ Step 1: Create Log Table
CREATE TABLE deleted_employees (
emp_id INT,
name VARCHAR(50),
deleted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
โ Step 2: Create Trigger
DELIMITER //
CREATE TRIGGER log_deleted_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO deleted_employees(emp_id, name)
VALUES (OLD.emp_id, OLD.name);
END //
DELIMITER ;
๐ง How It Works
๐ AFTER DELETE โ runs automatically after deletion
๐ OLD.emp_id and OLD.name
Access deleted row values before they disappear
โ Example
DELETE FROM employees
WHERE emp_id = 101;
โ Deleted employee info automatically saved in deleted_employees table ๐ฏ
๐ฅ Pro Tip
Triggers are powerful but:
โ Too many triggers can slow database
โ Use them carefully ๐ฏ
Double Tap โค๏ธ For More
โค5
๐๐ฅ๐๐ ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐ถ๐ฐ๐ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐ฏ๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ & ๐๐ถ๐ป๐ธ๐ฒ๐ฑ๐๐ป! ๐
Stop scrolling! This is your chance to get certified by two of the biggest names in techโ ๐ Level up your Data Skills for FREE!
โ What you get:
โข Official Microsoft & LinkedIn Certification
โข High-demand Data Analytics skills
โข Perfect for your Resume/LinkedIn profile
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4ubzzcC
๐Don't miss out on this career upgrade. Limited time offer!
Stop scrolling! This is your chance to get certified by two of the biggest names in techโ ๐ Level up your Data Skills for FREE!
โ What you get:
โข Official Microsoft & LinkedIn Certification
โข High-demand Data Analytics skills
โข Perfect for your Resume/LinkedIn profile
๐๐ป๐ฟ๐ผ๐น๐น ๐๐ผ๐ฟ ๐๐ฅ๐๐๐:-
https://pdlink.in/4ubzzcC
๐Don't miss out on this career upgrade. Limited time offer!
โค3
What is a Trigger in SQL?
Anonymous Quiz
5%
A. A table
14%
B. A stored query
79%
C. A block that executes automatically on database events
2%
D. An index
โค3
Which event can activate a trigger?
Anonymous Quiz
12%
A. INSERT
13%
B. UPDATE
1%
C. DELETE
73%
D. All of the above
What does NEW keyword represent in a trigger?
Anonymous Quiz
3%
A. Old row values
91%
B. New inserted/updated values
3%
C. Deleted values
3%
D. Table name
โค1