Comp SQL Project
Comp SQL Project
| DepartmentID | DepartmentName |
+--------------+----------------+
| 1 | HR |
| 2 | Engineering |
| 3 | Marketing |
pg. 1
+--------------+----------------+
-- Employees Table
SELECT * FROM Employees;
+------------+-----------+----------+--------------+---------+------------+
+------------+-----------+----------+--------------+---------+------------+
+------------+-----------+----------+--------------+---------+------------+
1. Retrieve the full name and salary of employees who earn more than $60,000.
FROM Employees
+-----------+----------+---------+
+-----------+----------+---------+
+-----------+----------+---------+
FROM Employees
pg. 2
WHERE YEAR(HireDate) = 2020;
+-----------+----------+------------+
+-----------+----------+------------+
+-----------+----------+------------+
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
+--------+------------------------------------------+----------+---------------+-----------------+
1. Retrieve the titles and authors of all books published after 1950.
3. List all authors who have written books in the 'Fantasy' genre.
1. Retrieve the product names and total quantities sold for each product.
pg. 4
+-------------+-------------------+
| Laptop | 2 |
| Smartphone | 5 |
| Tablet | 3 |
+-------------+-------------------+
4. Retrieve the products that have a stock count less than 60.
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
+-------------+-----------+----------+-------+
1. Retrieve the names of students and the courses they are enrolled in.
4. Retrieve the names of students who have received an 'A' grade in any course.
Tables
-- Create Patients table
CREATE TABLE Patients (
PatientID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE
);
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 |
+--------------+-----------+---------+----------------+----------------+
1. Retrieve the names of patients and the doctors they have appointments with.
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.
pg. 10