0% found this document useful (0 votes)
13 views10 pages

Comp SQL Project

Uploaded by

gangwaraabhas60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views10 pages

Comp SQL Project

Uploaded by

gangwaraabhas60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

SQL Program 1: Employee and Department

-- Create Employees table


CREATE TABLE Employees (EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT,
Salary DECIMAL(10, 2),
HireDate DATE);
-- Create Departments table
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50));

INSERT INTO Departments (DepartmentID, DepartmentName)


VALUES (1, 'HR'), (2, 'Engineering'), (3, 'Marketing');

INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID,


Salary, HireDate)
VALUES
(1, 'John', 'Doe', 1, 60000, '2020-01-15'),
(2, 'Jane', 'Smith', 2, 80000, '2019-04-23'),
(3, 'Sam', 'Brown', 3, 55000, '2021-06-12');
-- Departments Table
SELECT * FROM Departments;
+--------------+----------------+

| DepartmentID | DepartmentName |

+--------------+----------------+

| 1 | HR |

| 2 | Engineering |

| 3 | Marketing |

pg. 1
+--------------+----------------+

-- Employees Table
SELECT * FROM Employees;
+------------+-----------+----------+--------------+---------+------------+

| EmployeeID | FirstName | LastName | DepartmentID | Salary | HireDate |

+------------+-----------+----------+--------------+---------+------------+

| 1 | John | Doe | 1 | 60000.00| 2020-01-15 |

| 2 | Jane | Smith | 2 | 80000.00| 2019-04-23 |

| 3 | Sam | Brown | 3 | 55000.00| 2021-06-12 |

+------------+-----------+----------+--------------+---------+------------+

Questions and Outputs

1. Retrieve the full name and salary of employees who earn more than $60,000.

SELECT FirstName, LastName, Salary

FROM Employees

WHERE Salary > 60000;

+-----------+----------+---------+

| FirstName | LastName | Salary |

+-----------+----------+---------+

| Jane | Smith | 80000.00|

+-----------+----------+---------+

Find the average salary for each department.

SELECT d.DepartmentName, AVG(e.Salary) AS AverageSalary


FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
+----------------+---------------+
| DepartmentName | AverageSalary |
+----------------+---------------+
| HR | 60000.00|
| Engineering | 80000.00|
| Marketing | 55000.00|
+----------------+---------------+

SELECT FirstName, LastName, HireDate

FROM Employees
pg. 2
WHERE YEAR(HireDate) = 2020;

+-----------+----------+------------+

| FirstName | LastName | HireDate |

+-----------+----------+------------+

| John | Doe | 2020-01-15 |

+-----------+----------+------------+

SQL Program 2: Library and Boooks


Tables
-- Create Books table
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100),
AuthorID INT,
PublishedYear INT,
Genre VARCHAR(50)
);

-- Create Authors table


CREATE TABLE Authors (
AuthorID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);

INSERT INTO Authors (AuthorID, FirstName, LastName)


VALUES (1, 'George', 'Orwell'), (2, 'Aldous', 'Huxley'), (3, 'J.K.', 'Rowling');

INSERT INTO Books (BookID, Title, AuthorID, PublishedYear, Genre)


VALUES
(1, '1984', 1, 1949, 'Dystopian'),
(2, 'Brave New World', 2, 1932, 'Science Fiction'),
(3, 'Harry Potter and the Philosopher\'s Stone', 3, 1997, 'Fantasy');

Output Data
-- Authors Table
SELECT * FROM Authors;
+----------+-----------+----------+
| AuthorID | FirstName | LastName |
+----------+-----------+----------+
| 1 | George | Orwell |
| 2 | Aldous | Huxley |
| 3 | J.K. | Rowling |
+----------+-----------+----------+
-- Books Table
SELECT * FROM Books;
+--------+------------------------------------------+----------+---------------+-----------------+
| BookID | Title | AuthorID | PublishedYear | Genre |
+--------+------------------------------------------+----------+---------------+-----------------+
| 1 | 1984 | 1 | 1949 | Dystopian |
| 2 | Brave New World | 2 | 1932 | Science Fiction |
| 3 | Harry Potter and the Philosopher's Stone | 3 | 1997 | Fantasy |

pg. 3
+--------+------------------------------------------+----------+---------------+-----------------+

Questions and Outputs

1. Retrieve the titles and authors of all books published after 1950.

SELECT b.Title, a.FirstName, a.LastName


FROM Books b
JOIN Authors a ON b.AuthorID = a.AuthorID
WHERE b.PublishedYear > 1950;
+------------------------------------------+-----------+----------+
| Title | FirstName | LastName |
+------------------------------------------+-----------+----------+
| Harry Potter and the Philosopher's Stone | J.K. | Rowling |
+------------------------------------------+-----------+----------+

2. Find the number of books in each genre.

SELECT Genre, COUNT(*) AS NumberOfBooks


FROM Books
GROUP BY Genre;
+-----------------+---------------+
| Genre | NumberOfBooks |
+-----------------+---------------+
| Dystopian | 1 |
| Science Fiction | 1 |
| Fantasy | 1 |
+-----------------+---------------+

3. List all authors who have written books in the 'Fantasy' genre.

SELECT DISTINCT a.FirstName, a.LastName


FROM Books b
JOIN Authors a ON b.AuthorID = a.AuthorID
WHERE b.Genre = 'Fantasy';
+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| J.K. | Rowling |
+-----------+----------+

SQL Program 3: Online Store and products


Questions and Outputs

1. Retrieve the product names and total quantities sold for each product.

SELECT p.ProductName, SUM(o.Quantity) AS TotalQuantitySold


FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY p.ProductName;
+-------------+-------------------+
| ProductName | TotalQuantitySold |

pg. 4
+-------------+-------------------+
| Laptop | 2 |
| Smartphone | 5 |
| Tablet | 3 |
+-------------+-------------------+

2. Find the total revenue generated for each product.

SELECT p.ProductName, SUM(o.Quantity * p.Price) AS TotalRevenue


FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
GROUP BY p.ProductName;
+-------------+--------------+
| ProductName | TotalRevenue |
+-------------+--------------+
| Laptop | 2000.00|
| Smartphone | 2500.00|
| Tablet | 900.00|
+-------------+--------------+

3. List all orders that were placed in January 2022.

SELECT OrderID, ProductID, Quantity, OrderDate


FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-01-31';
+---------+-----------+----------+------------+
| OrderID | ProductID | Quantity | OrderDate |
+---------+-----------+----------+------------+
| 1 | 1 | 2 | 2022-01-15 |
| 2 | 2 | 5 | 2022-01-20 |
| 3 | 3 | 3 | 2022-01-25 |
+---------+-----------+----------+------------+

4. Retrieve the products that have a stock count less than 60.

SELECT ProductName, Stock


FROM Products
WHERE Stock < 60;
+-------------+-------+
| ProductName | Stock |
+-------------+-------+
| Laptop | 50 |
+-------------+-------+

SQL Program 4: University Coursses and systems


Tables
-- Create Courses table
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
Credits INT
);

-- Create Students table


CREATE TABLE Students (
pg. 5
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
EnrollmentYear INT
);

-- Create Enrollments table


CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
Grade CHAR(1)
);

INSERT INTO Courses (CourseID, CourseName, Credits)


VALUES (1, 'Database Systems', 4), (2, 'Operating Systems', 3), (3, 'Data Structures',
4);

INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentYear)


VALUES
(1, 'Alice', 'Johnson', 2020),
(2, 'Bob', 'Lee', 2019),
(3, 'Charlie', 'Kim', 2021);

INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, Grade)


VALUES
(1, 1, 1, 'A'),
(2, 2, 1, 'B'),
(3, 3, 2, 'A'),
(4, 1, 3, 'B');

Output Data
-- Courses Table
SELECT * FROM Courses;
+----------+------------------+--------+
| CourseID | CourseName | Credits|
+----------+------------------+--------+
| 1 | Database Systems | 4 |
| 2 | Operating Systems| 3 |
| 3 | Data Structures | 4 |
+----------+------------------+--------+

-- Students Table
SELECT * FROM Students;
+-----------+-----------+----------+----------------+
| StudentID | FirstName | LastName | EnrollmentYear |
+-----------+-----------+----------+----------------+
| 1 | Alice | Johnson | 2020 |
| 2 | Bob | Lee | 2019 |
| 3 | Charlie | Kim | 2021 |
+-----------+-----------+----------+----------------+
-- Enrollments Table
SELECT * FROM Enrollments;
+-------------+-----------+----------+-------+
| EnrollmentID| StudentID | CourseID | Grade |
+-------------+-----------+----------+-------+
| 1 | 1 | 1 | A |
| 2 | 2 | 1 | B |
| 3 | 3 | 2 | A |
| 4 | 1 | 3 | B |

pg. 6
+-------------+-----------+----------+-------+

Questions and Outputs

1. Retrieve the names of students and the courses they are enrolled in.

SELECT s.FirstName, s.LastName, c.CourseName


FROM Enrollments e
JOIN Students s ON e.StudentID = s.StudentID
JOIN Courses c ON e.CourseID = c.CourseID;
+-----------+----------+------------------+
| FirstName | LastName | CourseName |
+-----------+----------+------------------+
| Alice | Johnson | Database Systems |
| Bob | Lee | Database Systems |
| Charlie | Kim | Operating Systems|
| Alice | Johnson | Data Structures |
+-----------+----------+------------------+

2. Find the average grade for each course.

SELECT c.CourseName, AVG(CASE e.Grade


WHEN 'A' THEN 4.0
WHEN 'B' THEN 3.0
WHEN 'C' THEN 2.0
WHEN 'D' THEN 1.0
WHEN 'F' THEN 0.0
END) AS AverageGrade
FROM Enrollments e
JOIN Courses c ON e.CourseID = c.CourseID
GROUP BY c.CourseName;
+------------------+--------------+
| CourseName | AverageGrade |
+------------------+--------------+
| Database Systems | 3.50 |
| Operating Systems| 4.00 |
| Data Structures | 3.00 |
+------------------+--------------+

3. List all students who enrolled in the year 2020.

SELECT FirstName, LastName


FROM Students
WHERE EnrollmentYear = 2020;
+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Alice | Johnson |
+-----------+----------+

4. Retrieve the names of students who have received an 'A' grade in any course.

SELECT DISTINCT s.FirstName, s.LastName


FROM Enrollments e
JOIN Students s ON e.StudentID = s.StudentID
WHERE e.Grade = 'A';
+-----------+----------+
| FirstName | LastName |
pg. 7
+-----------+----------+
| Alice | Johnson |
| Charlie | Kim |
+-----------+----------+

SQL Program 5: Hospital and Patient Management

Tables
-- Create Patients table
CREATE TABLE Patients (
PatientID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE
);

-- Create Doctors table


CREATE TABLE Doctors (
DoctorID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Specialty VARCHAR(50)
);

-- Create Appointments table


CREATE TABLE Appointments (
AppointmentID INT PRIMARY KEY,
PatientID INT,
DoctorID INT,
AppointmentDate DATE,
Reason VARCHAR(100)
);

INSERT INTO Patients (PatientID, FirstName, LastName, DateOfBirth)


VALUES
(1, 'Anna', 'Smith', '1990-05-15'),
(2, 'James', 'Williams', '1985-08-20'),
(3, 'Emily', 'Jones', '2000-03-10');

INSERT INTO Doctors (DoctorID, FirstName, LastName, Specialty)


VALUES
(1, 'Dr. John', 'Doe', 'Cardiology'),
(2, 'Dr. Mary', 'Brown', 'Neurology'),
(3, 'Dr. William', 'Johnson', 'Orthopedics');

INSERT INTO Appointments (AppointmentID, PatientID, DoctorID, AppointmentDate, Reason)


VALUES
(1, 1, 1, '2022-01-10', 'Regular Checkup'),
(2, 2, 2, '2022-01-15', 'Headache'),
(3, 3, 3, '2022-01-20', 'Knee Pain');

Output Data
-- Patients Table
SELECT * FROM Patients;
+-----------+-----------+----------+-------------+
| PatientID | FirstName | LastName | DateOfBirth |
+-----------+-----------+----------+-------------+
| 1 | Anna | Smith | 1990-05-15 |
pg. 8
| 2 | James | Williams | 1985-08-20 |
| 3 | Emily | Jones | 2000-03-10 |
+-----------+-----------+----------+-------------+
-- Doctors Table
SELECT * FROM Doctors;
+---------+-----------+----------+-------------+
| DoctorID| FirstName | LastName | Specialty |
+---------+-----------+----------+-------------+
| 1 | Dr. John | Doe | Cardiology |
| 2 | Dr. Mary | Brown | Neurology |
| 3 | Dr. William| Johnson | Orthopedics |
+---------+-----------+----------+-------------+
-- Appointments Table
SELECT * FROM Appointments;
+--------------+-----------+---------+----------------+----------------+
| AppointmentID| PatientID | DoctorID| AppointmentDate| Reason |
+--------------+-----------+---------+----------------+----------------+
| 1 | 1 | 1 | 2022-01-10 | Regular Checkup|
| 2 | 2 | 2 | 2022-01-15 | Headache |
| 3 | 3 | 3 | 2022-01-20 | Knee Pain |
+--------------+-----------+---------+----------------+----------------+

Questions and Outputs

1. Retrieve the names of patients and the doctors they have appointments with.

SELECT p.FirstName AS PatientFirstName, p.LastName AS PatientLastName, d.FirstName


AS DoctorFirstName, d.LastName AS DoctorLastName
FROM Appointments a
JOIN Patients p ON a.PatientID = p.PatientID
JOIN Doctors d ON a.DoctorID = d.DoctorID;
+----------------+----------------+----------------+---------------+
| PatientFirstName| PatientLastName| DoctorFirstName| DoctorLastName|
+----------------+----------------+----------------+---------------+
| Anna | Smith | Dr. John | Doe |
| James | Williams | Dr. Mary | Brown |
| Emily | Jones | Dr. William | Johnson |
+----------------+----------------+----------------+---------------+

2. Find the total number of appointments each doctor has.

SELECT d.FirstName, d.LastName, COUNT(a.AppointmentID) AS TotalAppointments


FROM Doctors d
JOIN Appointments a ON d.DoctorID = a.DoctorID
GROUP BY d.FirstName, d.LastName;
+-----------+----------+------------------+
| FirstName | LastName | TotalAppointments|
+-----------+----------+------------------+
| Dr. John | Doe | 1 |
| Dr. Mary | Brown | 1 |
| Dr. William| Johnson | 1 |
+-----------+----------+------------------+

3. List all appointments that occurred in January 2022.

SELECT AppointmentID, PatientID, DoctorID, AppointmentDate, Reason


FROM Appointments
WHERE AppointmentDate BETWEEN '2022-01-01' AND '2022-01-31';

pg. 9
+--------------+-----------+---------+----------------+----------------+
| AppointmentID| PatientID | DoctorID| AppointmentDate| Reason |
+--------------+-----------+---------+----------------+----------------+
| 1 | 1 | 1 | 2022-01-10 | Regular Checkup|
| 2 | 2 | 2 | 2022-01-15 | Headache |
| 3 | 3 | 3 | 2022-01-20 | Knee Pain |
+--------------+-----------+---------+----------------+----------------+

4. Retrieve the names of doctors who have appointments with patients born after 1990.

SELECT DISTINCT d.FirstName, d.LastName


FROM Appointments a
JOIN Patients p ON a.PatientID = p.PatientID
JOIN Doctors d ON a.DoctorID = d.DoctorID
WHERE p.DateOfBirth > '1990-01-01';
+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Dr. William| Johnson |
+-----------+----------+

pg. 10

You might also like