Ex - No:4 Query The Database Tables and Explore Sub Queries and Simple Join Operations Date
Ex - No:4 Query The Database Tables and Explore Sub Queries and Simple Join Operations Date
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:
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);
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
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.