0% found this document useful (0 votes)
10 views8 pages

Ex - No:4 Query The Database Tables and Explore Sub Queries and Simple Join Operations Date

The document outlines the creation of a database with two tables: employees and departments, and demonstrates various SQL JOIN operations including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. It also includes subqueries to retrieve specific employee information based on salary and department criteria. The aim is to successfully execute SQL joins to retrieve information from the database.

Uploaded by

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

Ex - No:4 Query The Database Tables and Explore Sub Queries and Simple Join Operations Date

The document outlines the creation of a database with two tables: employees and departments, and demonstrates various SQL JOIN operations including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. It also includes subqueries to retrieve specific employee information based on salary and department criteria. The aim is to successfully execute SQL joins to retrieve information from the database.

Uploaded by

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

Ex.

No:4 QUERY THE DATABASE TABLES AND EXPLORE SUB QUERIES


Date: AND SIMPLE JOIN OPERATIONS

Aim:
Creation of a database and execute SQL Joins to retrieve information from the database.
QUERIES:
A SQL JOIN is an operation that combines rows from two or more tables based on a related
column between them. It's used to retrieve data from multiple tables in a relational database by
matching records based on a shared attribute or key (usually a column).
Types:
 INNER JOIN
 LEFT JOIN (or LEFT OUTER JOIN)
 RIGHT JOIN (or RIGHT OUTER JOIN)
 FULL OUTER JOIN

CREATE TABLE:

1.Table Creation : Employee table

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
years_of_experience INT
);

INSERT INTO employees (employee_id, first_name, last_name, department, salary,


years_of_experience)
VALUES
(1, 'KANI', 'MOZHI', 'Sales', 60000.00, 5),
(2, 'NILA', 'VENI', 'Marketing', 55000.00, 3),
(3, 'SIVA', 'KUAMAR', 'Sales', 70000.00, 7),
(4, 'RAGAV', 'DEV', 'IT', 80000.00, 4),
(5, 'NADHA', 'K', 'HR', 50000.00, 2) ,
(6, 'DEVA', 'KUMAR', 'Sales', 65000.00, 6),
(7, 'NILAN', 'N', 'Marketing', 57000.00, 4),
(8, 'DAVID', 'DAVIS', 'IT', 75000.00, 5),
(9, 'Henry', 'K', 'Sales', 72000.00, 8),
(10, 'MARTIN', 'M', 'HR', 52000.00, 3),
(11, 'HARI', 'DASS', 'Marketing', 60000.00, 6),
(12, 'JOHN', 'A', 'Sales', 68000.00, 7),
(13, 'LEO', 'THOMAS', 'IT', 77000.00, 6),
(14, 'MONA', 'J', 'HR', 54000.00, 4),
(15, 'NINI', 'DEV', 'Marketing', 59000.00, 3);
SELECT * FROM employees

2.Table Creation : departments table

CREATE TABLE departments (


department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);

insert into departments(department_id, department_name) values (101, 'Sales'),


(102,'Marketing'),(103,'IT'), (104,'HR');

select *from departments

Simple JOIN Query:

SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name
FROM
employees e
INNER JOIN
departments d
ON
e.department = d.department_name;

2.SUBQUERY: Retrieve Employees with Salaries Higher than the Average Salary

SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary > (SELECT AVG(salary) FROM employees);

3. Subquery with IN - Find Employees in Specific Departments

SELECT
employee_id,
first_name,
last_name,
department
FROM
employees
WHERE
department IN ('HR', 'IT', 'Sales');
RESULT:

Thus the Creation of a database and execute SQL Joins to retrieve information from the
database successfully.
Ex.No:5 QUERY THE DATABASE TABLES AND EXPLORE NATURAL ,
Date: EQUI AND OUTER JOIN OPERATIONS

AIM:

Creation of a database and execute SQL Joins to retrieve information from the database.

QUERIES:

JOIN TYPES
1. OUTER JOIN
a. LEFT OUTER JOIN
b. RIGHT OUTER JOIN
c. CROSS OUTER JOIN
2. EQUI JOIN
3. NON EQUI JOIN

1a. LEFT OUTER JOIN with Subquery:

SELECT
e.employee_id,
e.first_name,
e.last_name,
e.department
FROM
employees e
LEFT JOIN
departments d
ON
e.department = d.department_name;
1.b. Right Outer Join

SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name
FROM
departments d
RIGHT JOIN
employees e
ON
e.department = d.department_name;

1c. FULL OUTER JOIN to Get Employees and Departments (All Matches)

SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name
FROM
employees e
FULL OUTER JOIN
departments d
ON
e.department = d.department_name;
1d. INNER JOIN with Filtering on Department

SELECT
e.employee_id,
e.first_name,
e.last_name,
e.department,
e.salary
FROM
employees e
INNER JOIN
departments d
ON
e.department = d.department_name
WHERE
e.department = 'IT';

2.EQUI JOIN:

QUERY:
SELECT
e.employee_id,
e.first_name,
e.last_name,
e.salary,
d.department_name,
d.department_id
FROM
employees e
JOIN
departments d
ON
e.department = d.department_name;
3.NON EQUI JOIN

SELECT
e.employee_id,
e.first_name,
e.last_name,
e.salary,
d.department_name,
d.department_id
FROM
employees e
JOIN
departments d
ON
e.salary > 60000;

Result:
Thus the SQL Joins to retrieve information from the database has been executed succefully.

You might also like