Create Sample Table – EMPLOYEE, DEPARTMENT, LOCATIONS,
CUSTOMERS, ORDERS, PRODUCTS, STUDENTS, SUBJECTS, COURSES –
And Then Write SQL Queries To Solve All Question.
CREATE TABLE EMPLOYEES (
employee_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25) UNIQUE,
phone_number VARCHAR2(20),
hire_date DATE NOT NULL,
job_title VARCHAR2(30) NOT NULL,
salary NUMBER(8, 2),
department_id NUMBER(4)
);
Insertion Query :-
Employees Table :-
1|Pa ge
CREATE TABLE DEPARTMENTS (
department_id NUMBER(4) PRIMARY KEY,
department_name VARCHAR2(30) NOT NULL,
location_id NUMBER(4)
);
Insertion Query :-
Department Table :-
CREATE TABLE LOCATIONS (
location_id NUMBER(4) PRIMARY KEY,
city VARCHAR2(30),
state_province VARCHAR2(25)
);
Insertion Query :-
Location Table :-
2|Pa ge
CREATE TABLE CUSTOMERS (
customer_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(25) NOT NULL,
username VARCHAR2(20) UNIQUE,
country VARCHAR2(20)
);
Insertion Query :-
Customer Table :-
CREATE TABLE ORDERS (
order_id NUMBER(8) PRIMARY KEY,
customer_id NUMBER(6),
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES CUSTOMERS(customer_id)
);
Insertion Query :-
3|Pa ge
Orders Table :-
CREATE TABLE PRODUCTS (
product_code VARCHAR2(10) PRIMARY KEY,
product_name VARCHAR2(50) NOT NULL,
supplier_id NUMBER(6),
price NUMBER(8, 2),
special_info VARCHAR2(100)
);
Insertion Query :-
Product Table :-
CREATE TABLE STUDENTS (
student_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(25) NOT NULL,
country VARCHAR2(20)
);
4|Pa ge
Insertion Query :-
Student Table :-
CREATE TABLE SUBJECTS (
subject_id NUMBER(4) PRIMARY KEY,
subject_name VARCHAR2(50) NOT NULL
);
Insertion Query :-
Subject Table :-
CREATE TABLE STUDENT_SUBJECTS (
student_id NUMBER(6),
subject_id NUMBER(4),
PRIMARY KEY (student_id, subject_id),
FOREIGN KEY (student_id) REFERENCES STUDENTS(student_id),
FOREIGN KEY (subject_id) REFERENCES SUBJECTS(subject_id)
);
5|Pa ge
Insertion Query :-
Student_Subject Table :-
CREATE TABLE COURSES (
course_id NUMBER(6) PRIMARY KEY,
course_name VARCHAR2(50) NOT NULL,
start_date DATE,
discount NUMBER(5, 2)
);
Insertion Query :-
Courses Table :-
6|Pa ge
Q.1 Write A Query To Display The Employee Names And Their Department
Names From The Tables Employees And Departments.
Query :-
SELECT
e.first_name,
e.last_name,
d.department_name
FROM
employees e
INNER JOIN
departments d ON e.department_id = d.department_id;
Output :-
Q.2 Display All Orders Along With The Corresponding Customer Names
From Orders And Customers Tables.
Query :-
SELECT
o.order_id,
c.first_name,
c.last_name,
o.order_date
FROM
orders o
INNER JOIN
customers c ON o.customer_id = c.customer_id; 7|Pa ge
Output :-
Q.3 Show All Departments And The Employees Working In Them, Including
Departments With No Employees.
Query :-
SELECT
d.department_name,
e.first_name,
e.last_name
FROM
departments d
LEFT OUTER JOIN
employees e ON d.department_id = e.department_id;
Output :-
8|Pa ge
Q.4 Retrieve All Products And Their Supplier Names, Including Products
With No Supplier Information.
Query :-
SELECT
p.product_name,
p.product_code,
s.supplier_name
FROM
products p
LEFT JOIN
suppliers s ON p.supplier_id = s.supplier_id
ORDER BY
p.product_name;
Output :-
9|Page
Q.5 Show All Possible Combinations Of Students And Subjects (Use CROSS
JOIN).
Query :-
SELECT
s.first_name || ' ' || s.last_name AS student_name,
sub.subject_name
FROM
students s
CROSS JOIN
subjects sub
ORDER BY
student_name, sub.subject_name;
Output :-
10 | P a g e
Q.6 Write A Query To Display Employee Names, Their Department Names,
And The Location Of Each Department (Involves Joining Employees,
Departments, And Locations Tables).
Query :-
SELECT
e.first_name AS employee_first_name,
e.last_name AS employee_last_name,
d.department_name,
[Link] AS department_city
FROM
employees e
INNER JOIN
departments d ON e.department_id = d.department_id
INNER JOIN
locations l ON d.location_id = l.location_id
ORDER BY
d.department_name, e.last_name;
Output :-
11 | P a g e
Q.7 Create A CHECK Constraint To Ensure That End_Date Is Always Greater
Than Start_Date.
Query :-
ALTER TABLE COURSES
ADD CONSTRAINT check_end_date_after_start
CHECK (end_date > start_date);
Q.8 Define A Constraint To Ensure That Discount Must Be Between 0 And 50.
Output :-
ALTER TABLE COURSES
ADD CONSTRAINT check_discount_range
CHECK (discount BETWEEN 0 AND 50);
12 | P a g e
Q.9 Find The Total Number Of Employees In Each Department Using GROUP
BY.
Query :-
SELECT
department_id,
COUNT(employee_id) AS total_employees
FROM
employees
GROUP BY
department_id
ORDER BY
department_id;
Output :-
13 | P a g e
Q.10 Display Departments Having More Than 5 Employees (Use HAVING)
Query :-
SELECT
d.department_name,
COUNT(e.employee_id) AS total_employees
FROM
employees e
INNER JOIN
departments d ON e.department_id = d.department_id
GROUP BY
d.department_name
HAVING
COUNT(e.employee_id) > 5;
Output :-
14 | P a g e
Q. 11 Retrieve The Average Salary Of Employees Grouped By Job Title.
Query :-
SELECT
job_title,
ROUND(AVG(salary), 2) AS average_salary
FROM
employees
GROUP BY
job_title
ORDER BY
average_salary DESC;
Output :-
15 | P a g e
Q.12 Show The Department With The Maximum Number Of Employees.
Query :-
SELECT
department_name,
total_employees
FROM
(
SELECT
d.department_name,
COUNT(e.employee_id) AS total_employees
FROM
employees e
INNER JOIN
departments d ON e.department_id = d.department_id
GROUP BY
d.department_name
ORDER BY
total_employees DESC
);
Output :-
16 | P a g e
Q.13 Display All Customers Whose First Name Starts With ‘S’ And Last
Name Starts With ‘K’.
Query :-
SELECT
first_name,
last_name,
country
FROM
customers
WHERE
first_name LIKE 'S%'
AND last_name LIKE 'K%';
Output :-
17 | P a g e
Q.14 Find all usernames where the third character is ‘x’.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output:-
18 | P a g e
Q.15 Retrieve All Product Codes That Follow The Pattern: Start With ‘PRD’ &
Followed By Exactly 3 Digits.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
19 | P a g e
Q.16 Find All Departments O ered Either In Course Or Student.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
20 | P a g e
Q.17 Find All Departments That Have Both Students And Instructors.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
21 | P a g e
Q.18 Find Departments That Have Students But No Instructors.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
22 | P a g e
Q.19 Create Two Tables — Departments And Employees — Where Each
Employee Belongs To One Department.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
23 | P a g e
Q.20 Insert Sample Data Into Both Tables Ensuring Referential Integrity.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
24 | P a g e
Q.21 Write A Query To Add A NOT NULL Constraint To The Coursename
Column In The Courses Table.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
25 | P a g e
Q.22 Write A Query To Change The Default Value Of Fees To 25000.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
26 | P a g e
Q.23 Write A Query To Delete The Foreign Key Constraint From The Students
Table.
Query :-
SELECT
username
FROM
customers
WHERE
username LIKE '__x%';
Output :-
27 | P a g e
Q.24 Create A Foreign Key With ON DELETE SET NULL Action.
Query :-
Output :-
28 | P a g e
29 | P a g e