Unit 4 Dbms
Unit 4 Dbms
Unit 4 Dbms
Join Operators:
The SQL JOIN statement is used to combine rows from two tables based on a common
column and selects records that have matching values in these columns.
Syntax
The SQL JOIN syntax typically includes the JOIN clause, which specifies the tables to
combine and the related columns. The basic syntax is as follows:
Types of joins
Different Types of SQL JOIN
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records
from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
(INNER) JOIN:
Syntax
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
Example
Products
ProductI ProductName
D
1 Pen
2 Pencil
3 Scale
. Prices
ProductI Price
D
1 20
2 10
FROM Products
ON Products.ProductID = Prices.ProductID;
In this example, the inner join combines the Products and Prices tables based on the
ProductID column. The query will return all records from both tables with matching values in
the ProductID column.
ProductNam Price
e
Pen 20
Pencil 10
Returns all records from the left table, and the matched records from the right table.
Syntax
SELECT table1.column1, table2.column2
FROM table1
ON table1.column1 = table2.column2;
RIGHT (OUTER) JOIN:
Returns all records from the right table, and the matched records from the left table
Syntax
FROM table1
ON table1.column_name = table2.column_name;
Example
FULL (OUTER) JOIN:
Returns all records when there is a match in either left or right table.
Syntax
SELECT *
FROM table_1
ON table_1.column_name=table_2.column_name;
Example
What Is A Cross-Join In SQL?
A cross-join in SQL is a type of join operation that combines every row from one table with
every row from another, resulting in a Cartesian product of the two tables.
Syntax
If table1 has m rows and table2 has n rows, the result set will have m * n rows.
Example
Natural Join
Natural Join in SQL combines records from two or more tables based on the common column
between them.
The common column must have the same name and data type in both the tables.
There is no need to use the ON clause for join condition in natural join.
Syntax
SELECT * FROM
Let us create two tables employee and department and insert some data into it. We will then
implement natural join on these two tables.
EmployeeID varchar(10),
FirstName varchar(50),
DeptID varchar(10)
);
Output
|:----------:---------:------
DeptID varchar(10),
DeptName varchar(40),
);
VALUES(“D1001”, “Technology”),
(“D1002”, “Technology”),
Output
| DeptID | DeptName |
|:------:----------:
| D1001 | Technology |
| D1002 | Technology |
Output
|:----------:---------:--------:------:----------:---------
An SQL subquery is nothing but a query inside another query. We use a subquery to fetch
data from two tables.
The WHERE clause in a subquery is used to filter the results of the inner query before those
results are used by the outer query. Here’s a clear example:
Example:
SELECT name
FROM employees
WHERE department_id IN (
SELECT id
FROM departments
);
Inner Query:
SELECT id
FROM departments
Outer Query
SELECT name
FROM employees
WHERE department_id IN (...);
This part retrieves the names of employees whose department Ids match those returned by the
inner query.
Summary:
The WHERE clause in the subquery ensures that only departments located in New York are
considered, filtering the results for the outer query.
IN clause
The IN clause in a subquery allows you to filter records based on a list of values returned by
the subquery. Here’s a straightforward example.
Example
Table: employees
Table: departments
Id department_name
1 HR
2 IT
3 Marketing
We want to find the names of employees who work in either the “HR” or “IT” departments.
SELECT name
FROM employees
WHERE department_id IN (
SELECT id
FROM departments
);
Explanation
The outer query selects employee names where the department_id matches any of the IDs
returned by the subquery.
This way, it effectively filters the employees to only those who belong to either the HR or IT
departments.
HAVING clause
The HAVING clause is used to filter groups after aggregation, and it can be utilized in both
regular subqueries and correlated subqueries. Let’s go through both concepts with examples.
Example
Table: employees
Table: departments
Id department_name
1 HR
2 IT
3 Marketing
Let’s say we want to find departments where the average salary of employees is greater than
$55000.
FROM employees
GROUP BY department_id
Explanation
Aggregation: The query groups employees by department_id and calculates the average
salary for each department.
HAVING: The HAVING clause filters the results to only include departments with an
average salary greater than $55,000.
The ANY and ALL operators in SQL are used to compare a value against a set of values
returned by a subquery. Here’s how they work, along with examples.
Using ANY
The ANY operator returns true if any of the subquery values meet the condition.
Example
Let’s find employees whose salary is greater than the salary of any employee in the IT
department.
SELECT name
FROM employees
SELECT salary
FROM employees
WHERE department_id = 2
);
Explanation
The outer query checks if an employee’s salary is greater than any of those salaries. If at least
one condition is true, the employee is included in the result.
Using ALL
The ALL operator returns true only if all of the subquery values meet the condition.
Example
Now, let’s find employees whose salary is greater than the salary of all employees in the HR
department.
SELECT name
FROM employees
SELECT salary
FROM employees
WHERE department_id = 1
);
Explanation
The outer query checks if an employee’s salary is greater than all of those salaries. If true for
every salary returned by the subquery, the employee is included in the result.
Summary
ANY: Returns true if at least one of the subquery values satisfies the condition.
ALL: Returns true only if all subquery values satisfy the condition.
Both operators allow you to compare a single value against a set of values from a subquery,
enabling complex filtering conditions in your queries.
SQL functions
SQL functions are built-in programs that a relational database application uses to manipulate
data in some way.
A function in SQL is a set of SQL statements that perform a specific task on data or database
objects:
Definition
A function is a reusable code block that’s created and stored in the database.
How it works
Functions are useful for code reusability, especially when you need to repeatedly write the
same SQL scripts.
How to call it
Types
There are many types of functions, including data definition language (DDL), data
manipulation language (DML), and data query language (DQL) functions.
SQL Server comes with built-in functions, but you can also create your own user-defined
functions.