MySQL Assignment – 1
Basic Questions
- Write SQL to create a database named shop.
- Create a table products with columns: id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), price DECIMAL(8,2).
- Insert 5 sample records into products.
- Write SQL to update the price of product with id = 2 to 450.75.
- Delete the product with id = 3.
- Select all products where price is greater than 200.
- Create a table customers with fields cust_id INT AUTO_INCREMENT PRIMARY KEY, cust_name VARCHAR(50), city VARCHAR(50).
- Insert 3 sample customers into customers.
- Write SQL to display only distinct city names from customers.
- Write SQL to count total number of products in the products table.
- Write SQL to find all books whose author name ends with “n”.
- Select books ordered by price in ascending order.
- Select the top 3 most expensive books.
- Count how many members are in the members
- Write SQL to display only unique author names from books.
- Write SQL to find the maximum price of a book.
- Write SQL to find the minimum price of a book.
- Write SQL to calculate the average price of books.
- Add a new column email VARCHAR(100) to the members
- Update the email of the member with member_id = 1 to ram@example.com.
Intermediate Questions
- Create a table orders with fields: order_id INT AUTO_INCREMENT PRIMARY KEY, cust_id INT, prod_id INT, order_date DATE.
- Insert 3 sample orders (use cust_id and prod_id from above tables).
- Write SQL to join customers and orders to show customer name with their order_id.
- Write SQL to join orders and products to show product name and price with order_id.
- Write SQL to show total amount spent by each customer (use SUM).
- Write SQL to find the most expensive product.
- Add a new column email VARCHAR(100) to customers.
- Update the email of a specific customer.
- Write SQL to delete all orders placed before 2024-01-01.
- Write SQL to show top 2 highest priced products.
- Write SQL to show the list of customers who placed more than 2 orders.
- Write SQL to find the product with the second highest price.
- Write SQL to calculate the total number of orders placed in each month of 2024.
- Write SQL to display customer names along with the total number of distinct products they purchased.
- Write SQL to find customers who never placed an order.
- Write SQL to fetch all products that were ordered at least once (INNER JOIN with orders).
- Write SQL to find the average order amount for each customer.
- Write SQL to get the names of customers who ordered products priced above 500.
- Write SQL to display the highest order amount placed by each customer.
- Write SQL to select all orders along with product names, sorted by order_date descending.
Advanced Questions
- Write SQL to create a table employees with columns: emp_id INT AUTO_INCREMENT PRIMARY KEY, emp_name VARCHAR(100), dept_id INT, salary DECIMAL(10,2).
- Insert 4 employees into employees table.
- Create a departments table with dept_id INT PRIMARY KEY AUTO_INCREMENT, dept_name VARCHAR(100) UNIQUE.
- Insert 3 sample departments.
- Add a foreign key dept_id in employees table referencing departments table.
- Write SQL to display employees with their department names.
- Write SQL to increase salary of employees in “Sales” department by 10%.
- Write SQL to delete all employees whose salary is less than 20000.
- Create a table payments with pay_id INT AUTO_INCREMENT PRIMARY KEY, emp_id INT, amount DECIMAL(10,2), pay_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
- Write SQL query to show total salary paid to each employee using GROUP BY.