Unit 5 Bcomcardbms 2024
Unit 5 Bcomcardbms 2024
Cross Join
Natural Join
inner join
Outer join
Left outer join
Right outer join
Full outer join
Cross Join
A CROSS JOIN in SQL returns the Cartesian product of two tables, meaning each row
from the first table is combined with every row from the second table.
This results in a large number of rows if both tables contain multiple records.
Syntax
1
CROSS JOIN Query
2
SELECT Products.ProductName, Colors.Color FROM Products CROSS
JOIN Colors;
Natural Join
A NATURAL JOIN in SQL is used to combine rows from two or more tables based on
common columns. It automatically matches columns with the same name and data type
from both tables.
Syntax:
SELECT * FROM table1 NATURAL JOIN table2;
3
SELECT * FROM Employees NATURAL JOIN Departments;
4
SELECT * FROM Students NATURAL JOIN Courses;
INNER JOIN
An INNER JOIN retrieves records that have matching values in both tables.
Syntax:
5
SELECT Employees.emp_id, Employees.name, Departments.dept_name
FROM Employees INNER JOIN Departments
ON Employees.dept_id = Departments.dept_id;
An OUTER JOIN returns matching rows from both tables and includes unmatched rows
from one or both tables, depending on whether it is a LEFT JOIN, RIGHT JOIN, or
FULL JOIN.
TYPES
Left outer join
Right outer join
Full outer join
6
Left Outer Join
Returns all records from the left table and matching records from the right table.
If there is no match, NULL values are returned.
Syntax:
7
FROM Employees
LEFT JOIN Departments
ON Employees.dept_id = Departments.dept_id;
Returns all records from the right table and matching records from the left table.
Syntax
8
ON Employees.dept_id = Departments.dept_id;
Returns all records from both tables, with NULLs in places where there is no
match.
9
Syntax:
10
**** Different types of joins End **********
Syntax:
SELECT table1.column, table2.column
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column
WHERE table1.column = 'some_value';
Example:
11
Tables:
Syntax:
SELECT table1.column, table2.column
FROM table1 INNER JOIN table2
ON table1.common_column = table2.common_column
WHERE table1.column IN ('value1', 'value2');
12
Example:
Syntax:
SELECT COUNT(table1.column)
FROM table1 JOIN table2
ON table1.common_column = table2.common_column
HAVING COUNT (table1.column) > some_value;
13
SELECT COUNT(Employees.emp_id) AS total_employees
FROM Employees JOIN Departments
ON Employees.dept_id = Departments.dept_id
HAVING COUNT(Employees.emp_id) > 3;
The ANY keyword is used to compare a value with at least one value from a subquery.
It works with comparison operators like >, <, =, etc.
Syntax:
SELECT column1
FROM table1 WHERE column1 > ANY (SELECT column2 FROM table2);
Example: Find employees who earn more than at least one IT employee
Tables:
Employees
14
SELECT name, salary
FROM Employees
WHERE salary > ANY (SELECT salary FROM Employees WHERE dept_id =
102);
15
5. Using ALL with JOIN
SELECT column1
FROM table1
WHERE column1 > ALL (SELECT column2 FROM table2);
Example:
Syntax:
Example
16
SELECT Employees.name, Departments.dept_name
FROM Employees
INNER JOIN Departments
ON Employees.dept_id = Departments.dept_id;
***** Join Sub Queries End( Where, IN, ALL, Any, Having********** end
SQL functions
Date/Time Functions
Numeric functions
String Functions
Conversion functions
Date/Time Functions
SQL provides various Date/Time functions to manipulate and retrieve date and time values.
17
1.NOW():
Returns the current date and time.
example
SELECT NOW();
Output
2017-01-13 08:03:52
2. CURDATE():
SELECT CURDATE();
Output:
2025-03-16
3. CURTIME():
18
Example:
SELECT CURTIME();
Output:
08:05:15
Example
Example
19
Example
20
****** Date and Time Functions End **************
example
example
Example
SELECT ROUND(4.567, 2) AS rounded_value;
21
4. TRUNCATE() – Truncate to a Specific Decimal Place
Example
5.POWER() – Exponentiation
Example
Example
SELECT SQRT(25) AS square_root;
22
7. EXP() – Exponential Function
Example
Example
Example
23
10. RAND() – Generate a Random Number
Example
Example
Example
SELECT LEAST(10, 20, 30, 40) AS min_value;
24
Explain in detail about String Functions in SQL
SQL provides various string functions to manipulate and process text data.
Example
SELECT LENGTH('Hello SQL') AS string_length;
Example
Example
25
4. LEFT() – Get Leftmost Characters
Example
Example
26
6. SUBSTRING() / SUBSTR() – Extract a Substring
Example
Example
Example
27
9. REVERSE() – Reverse a String
Example
Example
Example
Example
SQL provides various conversion functions to convert data types between strings,
numbers, and dates.
These functions help in handling and transforming data efficiently.
The CAST() function converts an expression from one data type to another.
29
Syntax:
CAST(expression AS target_data_type)
30
5. DATE_FORMAT() – Format Date as String
31