0% found this document useful (0 votes)
8 views31 pages

Unit 5 Bcomcardbms 2024

The document provides an overview of various SQL join types, including Cross Join, Natural Join, Inner Join, and Outer Joins (Left, Right, Full). It also explains SQL functions such as Date/Time, Numeric, String, and Conversion functions, detailing their syntax and examples. Additionally, it covers the use of keywords like WHERE, IN, HAVING, ANY, and ALL with joins to filter and manipulate data.

Uploaded by

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

Unit 5 Bcomcardbms 2024

The document provides an overview of various SQL join types, including Cross Join, Natural Join, Inner Join, and Outer Joins (Left, Right, Full). It also explains SQL functions such as Date/Time, Numeric, String, and Conversion functions, detailing their syntax and examples. Additionally, it covers the use of keywords like WHERE, IN, HAVING, ANY, and ALL with joins to filter and manipulate data.

Uploaded by

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

Explain in detail about different types of joins

SQL Join Operators

 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

SELECT * FROM TableA CROSS JOIN TableB;

1
CROSS JOIN Query

SELECT Students.Name, Subjects.Subject FROM Students CROSS JOIN


Subjects;

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;

Example 2: Joining Students and Courses

4
SELECT * FROM Students NATURAL JOIN Courses;

INNER JOIN
An INNER JOIN retrieves records that have matching values in both tables.

it requires explicitly specifying the columns used for joining.

Syntax:

SELECT table1.column_name, table2.column_name


FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;

Example 1: Joining Employees and Departments

5
SELECT Employees.emp_id, Employees.name, Departments.dept_name
FROM Employees INNER JOIN Departments
ON Employees.dept_id = Departments.dept_id;

OUTER JOIN in SQL

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:

SELECT table1.column_name, table2.column_name


FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;

Example: Joining Employees and Departments

SELECT Employees.emp_id, Employees.name, Departments.dept_name

7
FROM Employees
LEFT JOIN Departments
ON Employees.dept_id = Departments.dept_id;

Right Outer Join

Returns all records from the right table and matching records from the left table.

If there is no match, NULL values are returned.

Syntax

SELECT table1.column_name, table2.column_name


FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;

Example: Joining Employees and Departments


SELECT Employees.emp_id, Employees.name, Departments.dept_name
FROM Employees
RIGHT JOIN Departments

8
ON Employees.dept_id = Departments.dept_id;

FULL OUTER JOIN

Returns all records from both tables, with NULLs in places where there is no
match.

9
Syntax:

SELECT table1.column_name, table2.column_name


FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;

Example: Joining Employees and Departments

SELECT Employees.emp_id, Employees.name, Departments.dept_name


FROM Employees
FULL JOIN Departments
ON Employees.dept_id = Departments.dept_id;

10
**** Different types of joins End **********

Explain about Join Sub Queries using SQL

SQL Keywords Used with JOINs

Using Where with Join

Filters records after the JOIN is performed.

Syntax:
SELECT table1.column, table2.column
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column
WHERE table1.column = 'some_value';

Example:

Get employees who work in the IT department.

11
Tables:

SELECT Employees.name, Departments.dept_name


FROM Employees INNER JOIN Departments
ON Employees.dept_id = Departments.dept_id
WHERE Departments.dept_name = 'IT';

Using IN with JOIN

The IN clause filters records based on a list of values.

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:

Get employees who work in HR or IT.

SELECT Employees.name, Departments.dept_name


FROM Employees INNER JOIN Departments
ON Employees.dept_id = Departments.dept_id
WHERE Departments.dept_name IN ('HR', 'IT');

Using HAVING with JOIN

The HAVING clause filters after aggregation.

Syntax:

SELECT COUNT(table1.column)
FROM table1 JOIN table2
ON table1.common_column = table2.common_column
HAVING COUNT (table1.column) > some_value;

Example: Check if there are more than 3 employees in the company

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;

4. Using ANY with JOIN

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

The ALL clause compares a value with all values in a subquery.

SELECT column1
FROM table1
WHERE column1 > ALL (SELECT column2 FROM table2);

Example:

Get employees earning more than all IT employees.

SELECT name, salary


FROM Employees
WHERE salary > ALL (SELECT salary FROM Employees WHERE dept_id =
102);

6. Using FROM with JOIN

The FROM clause specifies the tables used in the query.

Syntax:

SELECT table1.column, table2.column


FROM table1
JOIN table2
ON table1.common_column = table2.common_column;

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():

Returns the current date.


Example

SELECT CURDATE();

Output:
2025-03-16

3. CURTIME():

Returns the current time.

18
Example:

SELECT CURTIME();

Output:

08:05:15

4. DATE_ADD() – Add Days to a Date

Adds a specific number of days to a date.

Example

SELECT DATE_ADD('2025-03-16', INTERVAL 5 DAY) AS new_date;

5. DATE_SUB() – Subtract Days from a Date

Subtracts a specific number of days from a date.

Example

SELECT DATE_SUB('2025-03-16', INTERVAL 3 DAY) AS new_date;

6. DATEDIFF() – Difference Between Two Dates

Returns the number of days between two dates.

19
Example

SELECT DATEDIFF('2025-03-16', '2025-03-01') AS days_difference;

7. DAYNAME() – Get the Name of the Day

Returns the name of the weekday for a given date.

SELECT DAYNAME('2025-03-16') AS weekday_name;

8. MONTHNAME() – Get the Name of the Month

Returns the name of the month for a given date.

SELECT MONTHNAME('2025-03-16') AS month_name;

9. YEAR() – Extract the Year from a Date

Returns the year from a date.

SELECT YEAR('2025-03-16') AS year_value;

20
****** Date and Time Functions End **************

Explain in detail about Numeric functions in SQL


SQL provides various numeric functions to perform calculations on numbers.

1. ABS() – Absolute Value

Returns the absolute (positive) value of a number.

example

SELECT ABS(-25) AS absolute_value;

2. FLOOR() – Round Down to Previous Integer

Returns the largest integer less than or equal to a given number.

example

SELECT FLOOR(4.9) AS floor_value;

3. ROUND() – Round to a Specified Decimal Place

Rounds a number to the nearest integer or given decimal places.

Example
SELECT ROUND(4.567, 2) AS rounded_value;
21
4. TRUNCATE() – Truncate to a Specific Decimal Place

Removes decimal places without rounding.

Example

SELECT TRUNCATE(4.567, 2) AS truncated_value;

5.POWER() – Exponentiation

Raises a number to a given power.

Example

SELECT POWER(2, 3) AS power_value;

6.SQRT() – Square Root

Returns the square root of a number.

Example
SELECT SQRT(25) AS square_root;

22
7. EXP() – Exponential Function

Returns e^x (where e ≈ 2.718).

Example

SELECT EXP(1) AS exp_value;

8.LOG() – Natural Logarithm

Returns the natural logarithm (log base e) of a number.

Example

SELECT LOG(10) AS log_value;

9. SIGN() – Get Sign of a Number

Returns 1 for positive, -1 for negative, and 0 for zero.

Example

SELECT SIGN(-25) AS sign_value;

23
10. RAND() – Generate a Random Number

Returns a random number between 0 and 1.

Example

SELECT RAND() AS random_number;

11. GREATEST() – Find the Maximum Value

Returns the largest value from a list.

Example

SELECT GREATEST(10, 20, 30, 40) AS max_value;

12. LEAST() – Find the Minimum Value

Returns the smallest value from a list.

Example
SELECT LEAST(10, 20, 30, 40) AS min_value;

****Numeric functions end ****************

24
Explain in detail about String Functions in SQL

SQL provides various string functions to manipulate and process text data.

1. LENGTH() – Get String Length

Returns the number of characters in a string (including spaces).

Example
SELECT LENGTH('Hello SQL') AS string_length;

2. UPPER() / UCASE() – Convert to Uppercase

Converts a string to uppercase.

Example

SELECT UPPER('hello sql') AS uppercase_text;

3. LOWER() / LCASE() – Convert to Lowercase

Converts a string to lowercase.

Example

SELECT LOWER('HELLO SQL') AS lowercase_text;

25
4. LEFT() – Get Leftmost Characters

Returns the first N characters from a string.

Example

SELECT LEFT('Database', 3) AS left_part;

5 RIGHT() – Get Rightmost Characters

Returns the last N characters from a string.

Example

SELECT RIGHT('Database', 4) AS right_part;

26
6. SUBSTRING() / SUBSTR() – Extract a Substring

Extracts a part of a string from a given position.

Example

SELECT SUBSTRING('Database Management', 10, 5) AS substring_text;

7. CONCAT() – Concatenate Strings

Joins two or more strings together.

Example

SELECT CONCAT('Hello', ' ', 'SQL') AS full_text;

8. TRIM() – Remove Leading & Trailing Spaces

Removes spaces from both sides of a string.

Example

SELECT TRIM(' SQL Functions ') AS trimmed_text;

27
9. REVERSE() – Reverse a String

Returns the reversed version of a string.

Example

SELECT REVERSE('SQL') AS reversed_text;

10. ASCII() – Get ASCII Code of a Character

Returns the ASCII value of the first character of a string.

Example

SELECT ASCII('A') AS ascii_value;

11. CHAR_LENGTH() – Get Number of Characters

Returns the number of characters in a string.

SELECT CHAR_LENGTH('SQL Functions') AS char_length;

12. LTRIM() – Remove Leading Spaces


28
Removes spaces from the left side of a string.

Example

SELECT LTRIM(' SQL Functions') AS left_trimmed;

13. RTRIM() – Remove Trailing Spaces

Removes spaces from the right side of a string.

Example

SELECT RTRIM('SQL Functions ') AS right_trimmed;

***** String Functions End *******

Explain in detail about conversion functions in SQL

SQL provides various conversion functions to convert data types between strings,
numbers, and dates.
These functions help in handling and transforming data efficiently.

1. CAST() – Convert Data Type

The CAST() function converts an expression from one data type to another.

29
Syntax:
CAST(expression AS target_data_type)

Example: Convert a number to a string


SELECT CAST(12345 AS CHAR) AS converted_value;

2. TO_CHAR() – Convert Number/Date to String


Converts numbers or dates to formatted strings.
SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD') AS formatted_date;

3. FORMAT() – Format Number with Comma Separators

Formats a number with commas and decimal places.

SELECT FORMAT(1234567.89, 2) AS formatted_number;

4. TRY_PARSE() – Convert String to Number or Date

Converts a string to int, float, or date safely.

SELECT TRY_PARSE('789' AS INT) AS converted_value;

30
5. DATE_FORMAT() – Format Date as String

Converts a date to a formatted string.


SELECT DATE_FORMAT('2025-03-16', '%M %d, %Y') AS formatted_date;

6. STR_TO_DATE() – Convert String to Date

Similar to TO_DATE(), but used in MySQL.

SELECT STR_TO_DATE('16-03-2025', '%d-%m-%Y') AS converted_date;

***** Conversion functions end ***************


**********Unit V Completed *******************

31

You might also like