SQL Zero to Advance
SQL Zero to Advance
IN
SQL
Wasim Patwari
Empowering Learners in Computer Science &
Data Analytics
WhatsApp:91- 9607157409
DAY 01
INTRODUCTION TO SQL AND RELATIONAL
DATABASES
Understand what SQL is and its role in managing
databases.
Learn about relational databases and their
components.
Study basic SQL commands: SELECT, INSERT, UPDATE,
DELETE.
Practice writing simple queries and retrieving data from
a database
Example:
Write a SQL query to retrieve all the columns from the
"customers" table.
Practice Questions:
1. Write a SQL query to insert a new record into the
"employees" table.
2. Write a SQL query to update the "quantity" column of the
"products" table to 10 where the "product_id" is 5.
3. Write a SQL query to delete all records from the "orders"
table where the "status" is 'cancelled'.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve all the columns from the
"employees" table where the "salary" is greater than 50000.
Practice Questions:
1. Write a SQL query to retrieve all the columns from the
"products" table where the "category" is 'Electronics' and the
"price" is less than 1000.
2. Write a SQL query to retrieve the names of all customers
from the "customers" table in alphabetical order.
3. Write a SQL query to retrieve the total number of orders
from the "orders" table.
Dataset:
Click Here to View Dataset in Excel Sheet
Practice Questions:
1. Write a SQL query to retrieve the product name, category,
and supplier name from the "products", "categories", and
"suppliers" tables, joining them on the appropriate columns.
2. Write a SQL query to retrieve the employee name and
department name from the "employees" and "departments"
tables, joining them on the "department_id" column.
3. Write a SQL query to retrieve the customer name and
order amount from the "customers" and "orders" tables,
joining them on the "customer_id" column, and only
including orders with amounts greater than 1000.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve the total number of orders for
each customer from the "orders" table.
Practice Questions:
1. Write a SQL query to retrieve the average price of products
in each category from the "products" table.
2. Write a SQL query to retrieve the maximum salary for each
department from the "employees" table.
3. Write a SQL query to retrieve the total revenue generated
by each customer from the "orders" and "order_items"
tables.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to update the "quantity" column of the
"products" table to 20 for all products with a price greater
than 100.
Practice Questions:
1. Write a SQL query to delete all records from the
"customers" table where the "last_login_date" is older than 1
year.
2. Write a SQL query to insert new records into the
"employees" table, selecting data from the
"temp_employees" table.
3. Write a SQL query to update the "discount" column of the
"orders" table by increasing it by 5% for all orders placed
before a specific date.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve all the customers whose names
start with 'J' and have a city containing 'York'.
Practice Questions:
1. Write a SQL query to retrieve all the products with a price
either above 1000 or below 500.
2. Write a SQL query to retrieve the employees who were
hired between a specific date range.
3. Write a SQL query to retrieve all the customers who do
not have a phone number specified in the database.
DATASET
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve the length of the product
names from the "products" table.
Practice Questions:
1. Write a SQL query to retrieve the current date and time.
2. Write a SQL query to retrieve the uppercase names of all
the employees from the "employees" table.
3. Write a SQL query to retrieve the average price of
products after applying a 10% discount from the "products"
table.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve all the products with a price
higher than the average price of all products
Practice Questions:
1. Write a SQL query to retrieve the names of all employees
who have a salary higher than the maximum salary of the
'Sales' department.
2. Write a SQL query to retrieve all the customers who have
placed an order after the latest order date for a specific
product.
3. Write a SQL query to retrieve all the products that belong
to categories with more than 10 products.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Create a view named "high_salary_employees" that
retrieves all the employees with a salary greater than 50000
from the "employees" table
Practice Questions:
1. Create a view named "order_summary" that retrieves the
total order amount and the number of orders for each
customer from the "orders" table.
2. Create an index on the "email" column of the "customers"
table for faster searching.
3. Create a view named "product_inventory" that retrieves
the product name and the available quantity for each
product from the "products" and "inventory" tables.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Create a table named "employees" with columns for
employee ID, name, and email, where the employee ID is the
primary key and the email must be unique.
Practice Questions:
1. Create a table named "orders" with columns for order ID,
customer ID, and order date, where the order ID is the
primary key and the customer ID references the
"customers" table.
2. Create a table named "products" with columns for
product ID, name, and price, where the product ID is the
primary key and the price cannot be null.
3. Create a table named "categories" with columns for
category ID and name, where the category ID is the
primary key and the name must be unique.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Alter the "employees" table to add a new column named
"address" of type VARCHAR(100).
Practice Questions:
1. Rename the table "customer_details" to "client_details".
2. Delete the "quantity" column from the "products" table.
3. Modify the "orders" table to change the data type of the
"order_date" column to DATE.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to retrieve all employees and their
respective managers from the "employees" table using a
self-join.
Practice Questions:
1. Write a SQL query to retrieve all the orders that do not
have any corresponding items in the "order_items" table
using a subquery.
2. Write a SQL query to retrieve all the products along with
the total quantity sold for each product from the "products"
and "order_items" tables using a join and subquery.
3. Write a SQL query to retrieve all the customers who have
placed an order in the same month and year as their
registration date.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Write a SQL query to start a transaction, update the
"inventory" table by reducing the quantity of a product, and
commit the transaction.
Practice Questions:
1. Write a SQL query to start a transaction, delete all records
from the "orders" table, and roll back the transaction.
2. Write a SQL query to update the "balance" column of the
"accounts" table by adding a specific amount for a specific
account, ensuring the consistency of the transaction.
3. Write a SQL query to lock a specific row in the
"employees" table to prevent other transactions from
modifying it.
Dataset:
Click Here to View Dataset in Excel Sheet
Example:
Create a stored procedure named "get_customer_orders"
that takes a customer ID as input and retrieves all the orders
placed by that customer.
Practice Questions:
1. Create a trigger named "update_inventory" that
automatically updates the quantity in the "inventory" table
when an order is placed.
2. Create a user-defined function named
"calculate_discount" that takes the order total as input and
returns the discount amount based on specific conditions.
3. Write a SQL query to optimize a slow-performing query
by adding appropriate indexes and rewriting the query
structure.
Dataset:
Click Here to View Dataset in Excel Sheet
Practice Questions:
"SQL Subqueries" on w3schools.com
(https://www.w3schools.com/sql/sql_subqueries.asp)
"Subqueries in SQL: A Complete Guide" on sqlshack.com
(https://www.sqlshack.com/subqueries-in-sql-a-
complete-guide/)
Example:
1. Retrieve all customers who have made at least one
purchase.
2. Find the names of customers who have not made any
purchases.
3. Get the order details for orders with a total quantity
greater than the average quantity of all orders.
Practice Questions:
"SQL Joins" on mode.com (https://mode.com/sql-
tutorial/sql-joins/)
"A Visual Explanation of SQL Joins" by Coding Horror
(https://blog.codinghorror.com/a-visual-explanation-
of-sql-joins/)
Example:
1. Retrieve the order details along with the customer names
for all orders.
2. Find the products and their corresponding categories.
3. Get a list of customers and their total order amounts.
10. Get a list of products and the total quantity ordered for
each product.
Dataset: Products (product_id, product_name),
Order_Items (order_id, product_id, quantity)
Practice Questions:
"SQL Wildcards" and "SQL Regular Expressions" on
tutorialspoint.com(https://www.tutorialspoint.com/sql/s
ql-regular-expressions.htm)
"Sorting Rows with NULL Values in SQL" on
stackoverflow.com(https://stackoverflow.com/question
s/18411081/sorting-rows-with-null-values-in-sql)
Example:
1. Retrieve all employees whose names start with 'J'.
2. Find the products with names containing the word 'red'.
3. Get the list of employees sorted by their hire date in
descending order.
5. Find the products with names starting with 'S' and ending
with 'e'.
Dataset: Products (product_id, product_name)
Practice Questions:
"SQL Aggregate Functions" on sqlservertutorial.net
(https://www.sqlservertutorial.net/sql-server-
aggregate-functions/)
"GROUP BY Clause" on geeksforgeeks.org
(https://www.geeksforgeeks.org/sql-group-by/)
Example:
1. Calculate the total order amount for each customer.
2. Find the average salary for each department.
3. Get the maximum and minimum quantities ordered for
each product.
5. Find the customers with the highest and lowest total order
amounts.
Dataset: Customers (customer_id, customer_name),
Orders (order_id, customer_id, order_amount) 6. Get the
maximum and minimum ages for each department.
Dataset: Employees (employee_id, employee_name, age,
department)
Practice Questions:
"SQL UPDATE Statement" and "SQL DELETE Statement" on
techonthenet.com
(https://www.techonthenet.com/sql/update.php)
"SQL Transactions" on tutorialspoint.com
(https://www.tutorialspoint.com/sql/sql-
transactions.htm)
Example:
1. Update the email address of a specific customer.
2. Delete all orders placed by a certain customer.
3. Insert a new product into the database and ensure
transactional integrity.
10. Update the order dates for all orders placed on weekends
to the following Monday.
Dataset: Orders (order_id, order_date)
Practice Questions:
"SQL Views" on oracle.com
(https://docs.oracle.com/en/database/oracle/oracle-
database/19/sqlrf/CREATE-VIEW.html)
"SQL Indexing and Performance Tuning" on
sqlshack.com (https://www.sqlshack.com/sql-
indexing-and-performance-tuning/)
Example:
1. Create a view to retrieve the list of products and their
quantities in stock.
2. Optimize a slow-performing query using proper indexing
techniques.
10. Create a view to display the total order amount for each
customer.
Dataset: Customers (customer_id, customer_name),
Orders (order_id, customer_id, order_amount)
Practice Questions:
"SQL Window Functions" on postgresql.org
(https://www.postgresql.org/docs/current/tutorial-
window.html)
"SQL Stored Procedures" on tutorialspoint.com
(https://www.tutorialspoint.com/sql/sql-stored-
procedures.htm)
Example:
1. Calculate the cumulative sales amount for each product
using a window function.
2. Create a stored procedure to insert a new customer into
the database.
Practice Questions:
"Database Normalization" on studytonight.com
(https://www.studytonight.com/dbms/database-
normalization.php)
"Advanced SQL" on tutorialspoint.com
(https://www.tutorialspoint.com/advanced_sql/index.ht
m)
Example:
1. Design a database schema for an online bookstore using
entity-relationship modeling.
2. Write a recursive SQL query to find all employees and
their subordinates in a hierarchical organization structure.
Practice Questions:
"Tips for Optimizing SQL Queries" on dev.to
(https://dev.to/techgirl1908/tips-for-optimizing-sql-
queries-28f3)
SQL Query Optimization Techniques" on
tutorialgateway.org
(https://www.tutorialgateway.org/sql-query-
optimization-techniques/)
Example:
1. Identify and eliminate redundant or unnecessary joins in
a complex query.
2. Rewrite a subquery as a join to improve query
performance.
3. Use appropriate indexes to optimize query execution
Practice Questions:
• "SQL Recursive Queries" on postgresql.org
(https://www.postgresql.org/docs/current/queries-
with.html)
"Introduction to Common Table Expressions" on
sqlshack.com (https://www.sqlshack.com/introduction-
to-common-table-expressions-ctes-in-sql-server/)
"Window Functions" on sqlite.org
(https://www.sqlite.org/windowfunctions.html)
Example:
1. Write a recursive SQL query to find all employees and their
subordinates in a hierarchical organization structure.
2. Use a common table expression to calculate the running
total of sales amounts for each product.
3. Apply window functions to calculate moving averages of
product ratings.