0% found this document useful (0 votes)
9 views13 pages

SQL Queries Assignment (1)

The document outlines multiple sets of SQL schemas and related questions for various entities such as Sailors, Employees, Students, Customers, and more. Each set includes table creation commands and a series of SQL queries aimed at manipulating and retrieving data from these tables. The queries cover operations like inserting, updating, deleting records, and using clauses like HAVING for data aggregation.

Uploaded by

kolapatigayatri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

SQL Queries Assignment (1)

The document outlines multiple sets of SQL schemas and related questions for various entities such as Sailors, Employees, Students, Customers, and more. Each set includes table creation commands and a series of SQL queries aimed at manipulating and retrieving data from these tables. The queries cover operations like inserting, updating, deleting records, and using clauses like HAVING for data aggregation.

Uploaded by

kolapatigayatri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Set 1:

Sailors, Reserves, and Boats


Schema:
Sailors: (sid, sname, rating, age)
Reserves: (sid, bid, day)
Boats: (bid, bname, color)

CREATE TABLE Sailors (


sid INT PRIMARY KEY,
sname VARCHAR(100),
rating INT,
age DECIMAL(3, 1)
);

CREATE TABLE Reserves (


sid INT,
bid INT,
day DATE,
PRIMARY KEY (sid, bid, day),
FOREIGN KEY (sid) REFERENCES Sailors(sid),
FOREIGN KEY (bid) REFERENCES Boats(bid)
);

CREATE TABLE Boats (


bid INT PRIMARY KEY,
bname VARCHAR(100),
color VARCHAR(50)
);

Questions:
Write a query to insert a new sailor into the Sailors table.
Write an SQL query to list all sailors with a rating greater than 5.
Write a query to find the names of sailors who reserved a boat on '2024-11-01'.
Use the HAVING clause to list boats with more than 2 reservations.
Write a query to update the rating of sailor 101 by 1.
Write a query to delete sailors who are older than 50 years.
Use the ALTER statement to add a column sailor_email to the Sailors table.
Write a query to find the average rating of sailors who have reserved a boat.
Write a query to select all sailors who have reserved a boat of color 'Red'.
Create a foreign key constraint on the Reserves table referring to the Boats
table.

Set 2: Employees, Departments, and Projects


Schema:
Employees: (emp_id, emp_name, emp_salary, dept_id)
Departments: (dept_id, dept_name)
Projects: (proj_id, proj_name, dept_id)

CREATE TABLE Departments (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(100) UNIQUE
);

CREATE TABLE Employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
emp_salary DECIMAL(10, 2),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);
CREATE TABLE Projects (
proj_id INT PRIMARY KEY,
proj_name VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);
Questions:
Write a query to insert a new employee into the Employees table.
Write a query to list all employees who work in the 'HR' department.
Write a query to update the salary of an employee whose emp_id is 101 by 10%.
Create a foreign key constraint on the Employees table referring to the
Departments table.
Write a query to find the average salary of employees in the 'Engineering'
department.
Use the HAVING clause to find departments with an average salary greater than
$50,000.
Write an SQL query to delete employees who were hired before '2020-01-01'.
Use the ALTER statement to add a new column emp_email to the Employees table.
Write a query to select all employees with a salary between 40,000 and 60,000.
Write a query to list all unique department names from the Departments table.

Set 3: Students, Courses, and Enrollments


Schema:
Students: (student_id, student_name, student_email)
Courses: (course_id, course_name, course_duration)
Enrollments: (student_id, course_id, enrollment_date)

CREATE TABLE Students (


student_id INT PRIMARY KEY,
student_name VARCHAR(100),
student_email VARCHAR(100) UNIQUE
);

CREATE TABLE Courses (


course_id INT PRIMARY KEY,
course_name VARCHAR(100),
course_duration INT
);

CREATE TABLE Enrollments (


student_id INT,
course_id INT,
enrollment_date DATE,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);

Questions:
Write a query to insert a new student into the Students table.
Write a query to list all courses with a duration greater than 6 weeks.
Write a query to update the course duration for 'Math' to 12 weeks.
Write a query to find all students enrolled in a course named 'Physics'.
Use the HAVING clause to find courses with more than 5 students enrolled.
Write a query to delete students who have never enrolled in a course.
Use the ALTER statement to add a new column student_address to the Students
table.
Write a query to select all students enrolled in courses of duration less than 6
weeks.
Write a query to find the number of students enrolled in each course.
Create a foreign key constraint between Enrollments and Courses.
Set 4: Customers, Orders, and Products
Schema:
Customers: (customer_id, customer_name, customer_email)
Orders: (order_id, customer_id, order_date)
Products: (product_id, product_name, product_price)

CREATE TABLE Customers (


customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
customer_email VARCHAR(100) UNIQUE
);

CREATE TABLE Orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);

CREATE TABLE Products (


product_id INT PRIMARY KEY,
product_name VARCHAR(100),
product_price DECIMAL(10, 2)
);
Questions:
Write a query to insert a new customer into the Customers table.
Write a query to list all products with a price greater than $50.
Write a query to update the price of product 101 by 15%.
Write a query to find the total number of orders placed by a specific customer.
Use the HAVING clause to list products that have been ordered more than 10
times.
Write a query to delete orders placed before '2023-01-01'.
Use the ALTER statement to add a new column customer_phone to the Customers
table.
Write a query to select all products that have been ordered at least once.
Write a query to list all unique product names from the Products table.
Create a foreign key constraint on the Orders table referring to the Customers
table.

Set 5: Authors, Books, and Libraries


Schema:
Authors: (author_id, author_name, author_email)
Books: (book_id, book_title, book_genre, author_id)
Libraries: (library_id, library_name, location)
SQL to Create Tables:
CREATE TABLE Authors (
author_id INT PRIMARY KEY,
author_name VARCHAR(100),
author_email VARCHAR(100) UNIQUE
);

CREATE TABLE Books (


book_id INT PRIMARY KEY,
book_title VARCHAR(100),
book_genre VARCHAR(50),
author_id INT,
FOREIGN KEY (author_id) REFERENCES Authors(author_id)
);

CREATE TABLE Libraries (


library_id INT PRIMARY KEY,
library_name VARCHAR(100),
location VARCHAR(100)
);
Questions:
Write a query to insert a new author into the Authors table.
Write a query to list all books written by a specific author.
Write a query to update the genre of a book with book_id 101.
Write a query to find all libraries that have books in the 'Science Fiction'
genre.
Use the HAVING clause to find authors who have published more than 3 books.
Write a query to delete books that have not been borrowed in the past year.
Use the ALTER statement to add a new column author_bio to the Authors table.
Write a query to select all authors who write books in the 'Fantasy' genre.
Write a query to find the average number of books published by each author.
Create a foreign key constraint on the Books table referring to the Authors
table.

Set 6: Vehicles, Owners, and Insurance


Schema:
Vehicles: (vehicle_id, vehicle_make, vehicle_model, vehicle_year)
Owners: (owner_id, owner_name, owner_email)
Insurance: (insurance_id, vehicle_id, start_date, end_date)
SQL to Create Tables:

CREATE TABLE Vehicles (


vehicle_id INT PRIMARY KEY,
vehicle_make VARCHAR(100),
vehicle_model VARCHAR(100),
vehicle_year INT
);

CREATE TABLE Owners (


owner_id INT PRIMARY KEY,
owner_name VARCHAR(100),
owner_email VARCHAR(100) UNIQUE
);

CREATE TABLE Insurance (


insurance_id INT PRIMARY KEY,
vehicle_id INT,
start_date DATE,
end_date DATE,
FOREIGN KEY (vehicle_id) REFERENCES Vehicles(vehicle_id)
);

Questions:
Write a query to insert a new vehicle into the Vehicles table.
Write a query to list all vehicles older than 10 years.
Write a query to find the owner of a specific vehicle with vehicle_id 101.
Write a query to find the insurance status for a vehicle.
Use the HAVING clause to list vehicles that have had insurance for more than 2
years.
Write a query to delete insurance records that expired before '2024-01-01'.
Use the ALTER statement to add a new column vehicle_color to the Vehicles table.
Write a query to select all owners who own vehicles older than 5 years.
Write a query to find the total number of insurance policies associated with
each vehicle.
Create a foreign key constraint on the Insurance table referring to the Vehicles
table.

Set 7: Orders, Products, and OrderDetails


Schema:
Orders: (order_id, customer_id, order_date)
Products: (product_id, product_name, product_price)
OrderDetails: (order_id, product_id, quantity)
SQL to Create Tables:

CREATE TABLE Orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE
);

CREATE TABLE Products (


product_id INT PRIMARY KEY,
product_name VARCHAR(100),
product_price DECIMAL(10, 2)
);

CREATE TABLE OrderDetails (


order_id INT,
product_id INT,
quantity INT,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);
Questions:
Write a query to insert a new product into the Products table.
Write a query to find all orders that contain a specific product.
Write a query to update the quantity of a product in a specific order.
Write a query to find the total amount spent in each order.
Use the HAVING clause to list orders with total quantities greater than 10.
Write a query to delete products from the order where quantity is less than 5.
Use the ALTER statement to add a new column order_status to the Orders table.
Write a query to select all products that have been ordered at least once.
Write a query to find the average price of products sold in each order.
Create a foreign key constraint between OrderDetails and Orders.

Set 8: Employees, Salaries, and Departments


Schema:
Employees: (emp_id, emp_name, emp_salary, dept_id)
Salaries: (emp_id, salary_date, salary_amount)
Departments: (dept_id, dept_name)

CREATE TABLE Employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
emp_salary DECIMAL(10, 2),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);

CREATE TABLE Salaries (


emp_id INT,
salary_date DATE,
salary_amount DECIMAL(10, 2),
PRIMARY KEY (emp_id, salary_date),
FOREIGN KEY (emp_id) REFERENCES Employees(emp_id)
);

CREATE TABLE Departments (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(100) UNIQUE
);
Questions:
Write a query to insert a new employee into the Employees table.
Write a query to list all employees who earn more than $50,000.
Write a query to find the salary history for employee 101.
Write a query to update the salary of an employee.
Use the HAVING clause to find departments with an average salary greater than
$60,000.
Write a query to delete employees who have a salary lower than $40,000.
Use the ALTER statement to add a column emp_phone to the Employees table.
Write a query to select employees with salaries between $40,000 and $70,000.
Write a query to find the average salary per department.
Create a foreign key constraint on Salaries referring to Employees.

Set 9: Customers, Products, and Orders


Schema:
Customers: (customer_id, customer_name, customer_email)
Products: (product_id, product_name, product_price)
Orders: (order_id, customer_id, order_date)
SQL to Create Tables:
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
customer_email VARCHAR(100) UNIQUE
);

CREATE TABLE Products (


product_id INT PRIMARY KEY,
product_name VARCHAR(100),
product_price DECIMAL(10, 2)
);

CREATE TABLE Orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
Questions:
Write a query to insert a new customer into the Customers table.
Write a query to list all products with a price greater than $100.
Write a query to update the price of a product.
Write a query to list all orders placed by customer 101.
Use the HAVING clause to list products that have been ordered more than 10
times.
Write a query to delete orders placed before '2024-01-01'.
Use the ALTER statement to add a new column order_status to the Orders table.
Write a query to find the total amount spent by each customer.
Write a query to find the number of products ordered by each customer.
Create a foreign key constraint between Orders and Customers.

Set 10: Students, Professors, and Courses


Schema:
Students: (student_id, student_name, student_email)
Professors: (professor_id, professor_name, professor_email)
Courses: (course_id, course_name, professor_id)

CREATE TABLE Students (


student_id INT PRIMARY KEY,
student_name VARCHAR(100),
student_email VARCHAR(100) UNIQUE
);

CREATE TABLE Professors (


professor_id INT PRIMARY KEY,
professor_name VARCHAR(100),
professor_email VARCHAR(100) UNIQUE
);

CREATE TABLE Courses (


course_id INT PRIMARY KEY,
course_name VARCHAR(100),
professor_id INT,
FOREIGN KEY (professor_id) REFERENCES Professors(professor_id)
);

Questions:
Write a query to insert a new professor into the Professors table.
Write a query to list all students enrolled in a course taught by 'Prof. John'.
Write a query to update the course name for course_id 101.
Write a query to find all courses taught by a specific professor.
Use the HAVING clause to find professors who teach more than 3 courses.
Write a query to delete professors who have not taught any courses.
Use the ALTER statement to add a column professor_phone to the Professors table.
Write a query to select students enrolled in a course with more than 100
students.
Write a query to find the total number of courses taught by each professor.
Create a foreign key constraint on the Courses table referring to the Professors
table.

Set 11: Patients, Doctors, and Appointments


Schema:
Patients: (patient_id, patient_name, patient_dob, patient_email)
Doctors: (doctor_id, doctor_name, specialty)
Appointments: (appointment_id, patient_id, doctor_id, appointment_date)
SQL to Create Tables:

CREATE TABLE Patients (


patient_id INT PRIMARY KEY,
patient_name VARCHAR(100),
patient_dob DATE,
patient_email VARCHAR(100) UNIQUE
);

CREATE TABLE Doctors (


doctor_id INT PRIMARY KEY,
doctor_name VARCHAR(100),
specialty VARCHAR(100)
);

CREATE TABLE Appointments (


appointment_id INT PRIMARY KEY,
patient_id INT,
doctor_id INT,
appointment_date DATE,
FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),
FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)
);
Questions:
Write a query to insert a new patient into the Patients table.
Write a query to list all doctors who specialize in 'Cardiology'.
Write a query to find all appointments scheduled with a specific doctor.
Use the HAVING clause to find doctors who have more than 5 appointments in a
week.
Write a query to update the contact email of a patient.
Write a query to delete appointments scheduled before '2024-01-01'.
Use the ALTER statement to add a new column patient_phone to the Patients table.
Write a query to select all patients who have an appointment with a specific
doctor.
Write a query to find the total number of appointments for each doctor.
Create a foreign key constraint on the Appointments table referring to the
Doctors table.

Set 12: Products, Categories, and Suppliers


Schema:
Products: (product_id, product_name, price, category_id)
Categories: (category_id, category_name)
Suppliers: (supplier_id, supplier_name, contact_info)

CREATE TABLE Categories (


category_id INT PRIMARY KEY,
category_name VARCHAR(100)
);

CREATE TABLE Products (


product_id INT PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10, 2),
category_id INT,
FOREIGN KEY (category_id) REFERENCES Categories(category_id)
);

CREATE TABLE Suppliers (


supplier_id INT PRIMARY KEY,
supplier_name VARCHAR(100),
contact_info VARCHAR(100)
);
Questions:
Write a query to insert a new product into the Products table.
Write a query to list all products in the 'Electronics' category.
Write a query to update the price of a product with product_id 101.
Write a query to find products that cost more than $500.
Use the HAVING clause to find categories with an average product price greater
than $200.
Write a query to delete products that have a price lower than $10.
Use the ALTER statement to add a new column supplier_email to the Suppliers
table.
Write a query to select all products supplied by a specific supplier.
Write a query to find the total number of products in each category.
Create a foreign key constraint on the Products table referring to the
Categories table.

Set 13: Orders, Order_Items, and Customers


Schema:
Orders: (order_id, customer_id, order_date, status)
Order_Items: (order_item_id, order_id, product_id, quantity)
Customers: (customer_id, customer_name, customer_email)

CREATE TABLE Customers (


customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
customer_email VARCHAR(100) UNIQUE
);

CREATE TABLE Orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
status VARCHAR(50),
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);

CREATE TABLE Order_Items (


order_item_id INT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);

Questions:
Write a query to insert a new order into the Orders table.
Write a query to find all orders placed by a specific customer.
Write a query to update the status of an order to 'Shipped'.
Write a query to find the total quantity of products ordered in a specific
order.
Use the HAVING clause to find orders with more than 5 items.
Write a query to delete orders that were placed before '2023-01-01'.
Use the ALTER statement to add a new column delivery_address to the Orders
table.
Write a query to select all customers who have placed an order in the past
month.
Write a query to find the number of items in each order.
Create a foreign key constraint on the Order_Items table referring to the Orders
table.

Set 14: Movies, Actors, and Directors


Schema:
Movies: (movie_id, title, genre, release_year)
Actors: (actor_id, actor_name, birthdate)
Directors: (director_id, director_name, birthdate)

CREATE TABLE Movies (


movie_id INT PRIMARY KEY,
title VARCHAR(100),
genre VARCHAR(50),
release_year INT
);

CREATE TABLE Actors (


actor_id INT PRIMARY KEY,
actor_name VARCHAR(100),
birthdate DATE
);

CREATE TABLE Directors (


director_id INT PRIMARY KEY,
director_name VARCHAR(100),
birthdate DATE
);

Questions:
Write a query to insert a new movie into the Movies table.
Write a query to list all movies released after 2010.
Write a query to update the genre of the movie with movie_id 101.
Write a query to find all actors who acted in movies released after '2000'.
Use the HAVING clause to find genres with more than 5 movies.
Write a query to delete movies with a release year older than 1990.
Use the ALTER statement to add a new column director_id to the Movies table.
Write a query to select all movies directed by a specific director.
Write a query to find the average release year for movies in each genre.
Create a foreign key constraint on the Movies table referring to the Directors
table.

Set 15: Authors, Books, and Bookstore


Schema:
Authors: (author_id, author_name, nationality)
Books: (book_id, book_title, publication_year, author_id)
Bookstore: (store_id, store_name, location)
SQL to Create Tables:

CREATE TABLE Authors (


author_id INT PRIMARY KEY,
author_name VARCHAR(100),
nationality VARCHAR(50)
);

CREATE TABLE Books (


book_id INT PRIMARY KEY,
book_title VARCHAR(100),
publication_year INT,
author_id INT,
FOREIGN KEY (author_id) REFERENCES Authors(author_id)
);

CREATE TABLE Bookstore (


store_id INT PRIMARY KEY,
store_name VARCHAR(100),
location VARCHAR(100)
);
Questions:
Write a query to insert a new author into the Authors table.
Write a query to list all books published before the year 2000.
Write a query to find all books by authors from 'USA'.
Use the HAVING clause to find bookstores with more than 10 different books in
stock.
Write a query to update the publication year of a book.
Write a query to delete authors who have not published any books.
Use the ALTER statement to add a new column book_price to the Books table.
Write a query to select all books sold by a specific bookstore.
Write a query to find the average number of books published by each author.
Create a foreign key constraint on the Books table referring to the Authors
table.

Set 16: Employees, Projects, and Salaries


Schema:
Employees: (emp_id, emp_name, job_title)
Projects: (project_id, project_name, start_date, end_date)
Salaries: (emp_id, salary_amount, salary_date)

CREATE TABLE Employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
job_title VARCHAR(100)
);

CREATE TABLE Projects (


project_id INT PRIMARY KEY,
project_name VARCHAR(100),
start_date DATE,
end_date DATE
);
CREATE TABLE Salaries (
emp_id INT,
salary_amount DECIMAL(10, 2),
salary_date DATE,
FOREIGN KEY (emp_id) REFERENCES Employees(emp_id)
);
Questions:
Write a query to insert a new employee into the Employees table.
Write a query to list all employees working on a project that started after
'2023-01-01'.
Write a query to find the total salary of all employees in a specific month.
Write a query to update the job title of an employee.
Use the HAVING clause to find employees whose salary is greater than $60,000.
Write a query to delete employees whose salary is below $30,000.
Use the ALTER statement to add a new column hire_date to the Employees table.
Write a query to select all employees working on a project that ends after
'2024-01-01'.
Write a query to find the highest salary paid to employees in each department.
Create a foreign key constraint on the Salaries table referring to the Employees
table.

Set 17: Vehicles, Rentals, and Customers


Schema:
Vehicles: (vehicle_id, vehicle_type, registration_number)
Rentals: (rental_id, vehicle_id, customer_id, rental_date)
Customers: (customer_id, customer_name, contact_info)
SQL to Create Tables:

CREATE TABLE Vehicles (


vehicle_id INT PRIMARY KEY,
vehicle_type VARCHAR(100),
registration_number VARCHAR(50) UNIQUE
);

CREATE TABLE Rentals (


rental_id INT PRIMARY KEY,
vehicle_id INT,
customer_id INT,
rental_date DATE,
FOREIGN KEY (vehicle_id) REFERENCES Vehicles(vehicle_id),
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);

CREATE TABLE Customers (


customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
contact_info VARCHAR(100)
);
Questions:
Write a query to insert a new vehicle into the Vehicles table.
Write a query to find all rentals for a specific vehicle.
Write a query to update the rental date of a specific rental.
Write a query to list all customers who rented a vehicle after '2024-01-01'.
Use the HAVING clause to find vehicles that were rented more than 3 times in the
past month.
Write a query to delete rentals that occurred before '2024-01-01'.
Use the ALTER statement to add a new column vehicle_color to the Vehicles table.
Write a query to find all vehicles rented by a specific customer.
Write a query to find the total number of rentals for each vehicle.
Create a foreign key constraint on the Rentals table referring to the Customers
table.
Set 18: Flights, Airlines, and Passengers
Schema:
Flights: (flight_id, flight_number, departure_time, arrival_time)
Airlines: (airline_id, airline_name, country)
Passengers: (passenger_id, passenger_name, passport_number)
SQL to Create Tables:

CREATE TABLE Airlines (


airline_id INT PRIMARY KEY,
airline_name VARCHAR(100),
country VARCHAR(50)
);

CREATE TABLE Flights (


flight_id INT PRIMARY KEY,
flight_number VARCHAR(50),
departure_time DATETIME,
arrival_time DATETIME,
airline_id INT,
FOREIGN KEY (airline_id) REFERENCES Airlines(airline_id)
);

CREATE TABLE Passengers (


passenger_id INT PRIMARY KEY,
passenger_name VARCHAR(100),
passport_number VARCHAR(100) UNIQUE
);

Questions:
Write a query to insert a new airline into the Airlines table.
Write a query to list all flights operated by 'Airline A'.
Write a query to find the average duration of all flights.
Write a query to update the flight time for a specific flight.
Use the HAVING clause to find airlines with more than 5 flights.
Write a query to delete flights that are scheduled before '2024-01-01'.
Use the ALTER statement to add a new column passenger_email to the Passengers
table.
Write a query to select all passengers who are on flights arriving after '2024-
06-01'.
Write a query to find the longest flight based on departure and arrival time.
Create a foreign key constraint on the Flights table referring to the Airlines
table.

Set 19: Students, Classes, and Grades


Schema:
Students: (student_id, student_name, student_dob)
Classes: (class_id, class_name, teacher_name)
Grades: (student_id, class_id, grade)
CREATE TABLE Students (
student_id INT PRIMARY KEY,
student_name VARCHAR(100),
student_dob DATE
);

CREATE TABLE Classes (


class_id INT PRIMARY KEY,
class_name VARCHAR(100),
teacher_name VARCHAR(100)
);

CREATE TABLE Grades (


student_id INT,
class_id INT,
grade CHAR(2),
PRIMARY KEY (student_id, class_id),
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (class_id) REFERENCES Classes(class_id)
);
Questions:
Write a query to insert a new student into the Students table.
Write a query to list all classes taught by 'Mr. Smith'.
Write a query to find the grades of a specific student in all their classes.
Use the HAVING clause to find classes with an average grade greater than 'B'.
Write a query to update the grade of a student in a specific class.
Write a query to delete students who have failed more than 3 classes.
Use the ALTER statement to add a new column student_email to the Students table.
Write a query to select students who have passed all their classes.
Write a query to find the number of students enrolled in each class.
Create a foreign key constraint on the Grades table referring to the Students
table.

Set 20: Products, Orders, and Payments


Schema:
Products: (product_id, product_name, price)
Orders: (order_id, customer_id, order_date)
Payments: (payment_id, order_id, payment_amount, payment_date)
SQL to Create Tables:
Questions:
Write a query to insert a new product into the Products table.
Write a query to find all orders placed after '2024-01-01'.
Write a query to update the price of a product.
Write a query to list all payments for a specific order.
Use the HAVING clause to find orders with total payment greater than $1000.
Write a query to delete payments that occurred before '2024-01-01'.
Use the ALTER statement to add a new column customer_email to the Orders table.
Write a query to select all orders that have been paid for.
Write a query to find the total payment received for each order.
Create a foreign key constraint on the Payments table referring to the Orders
table.

You might also like