Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

SQL_part_2__1733732359

Uploaded by

gnan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_part_2__1733732359

Uploaded by

gnan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL PART -2

_____________________________________________________________

SQL JOINS
A join is a way to combine data from two or more database table based on
a related column between them.
Join are used when we want to query info that is distributed across multiple
tables in the database and the info we need is not contain in the single
table, by joining table together we can create a virtual table that contain all
the info we need for our query.

CROSS JOIN
A CROSS JOIN in SQL is used to create the Cartesian product of two tables,
meaning it combines each row of the first table with every row of the
second table. This type of join does not require any condition to match the
rows and is typically used to generate all possible combinations of rows
from the tables involved.
Cartesian product, Combination
SELECT * FROM table1 CROSS JOIN table2;

INNER JOIN
Combines rows from two tables based on a matching condition. Only
returns rows where there is a match in both tables.
Common records
SELECT columns
FROM table1
INNER JOIN table2 ON table1.common_column =
table2.common_column;
SELF JOIN
A SELF JOIN in SQL is a regular join where a table is joined with itself. It is
useful when you need to compare rows within the same table
Same table, Alias
SELECT a.column_name, b.column_name
FROM table_name a, table_name b
WHERE condition;

LEFT JOIN
A LEFT JOIN, also known as a LEFT OUTER JOIN, in SQL is used to combine
rows from two tables. It returns all rows from the left table and the matched
rows from the right table. If there is no match, the result is NULL on the side
of the right table.
Left table, Match, NULL
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column =
table2.common_column;

RIGHT JOIN
A RIGHT JOIN, also known as a RIGHT OUTER JOIN, in SQL is used to
combine rows from two tables. It returns all rows from the right table and
the matched rows from the left table. If there is no match, the result is
NULL on the side of the left table.
Right table, Match, NULL
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column =
table2.common_column;

FULLJOIN
A FULL JOIN, also known as a FULL OUTER JOIN, in SQL returns all rows
when there is a match in one of the tables. If there is no match, the result is
NULL on the side where there is no match. Essentially, it combines the
results of both a LEFT JOIN and a RIGHT JOIN.
Match, Both tables, NULL
SELECT columns
FROM table1
FULL JOIN table2 ON table1.common_column =
table2.common_column;

UNION
the UNION operator is used to combine the result sets of two or more
SELECT statements. It eliminates duplicate rows in the result set
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
UNION ALL
If you want to include duplicate rows in the result set, use UNION ALL:
SELECT emp_id, name, dept_id
FROM employees_2022
UNION ALL
SELECT emp_id, name, dept_id
FROM employees_2023;

INTERSECT
INTERSECT operator in SQL, which is used to return the common records
from two SELECT statements. It essentially finds the intersection of two
result sets, only returning the rows that appear in both queries.
SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;

EXCEPT
The EXCEPT operator in SQL is used to return rows from the first SELECT
statement that are not present in the second SELECT statement. It
essentially finds the difference between two result sets.
SELECT column1, column2, ...
FROM table1 EXCEPT
SELECT column1, column2, ...
FROM table2;
SUBQUERIES
• Subqueries, also known as inner queries or nested queries.
• They are used to perform operations that are based on the results of
another query.
• Subqueries can be found in various clauses like SELECT, FROM,
WHERE, and HAVING.
Types of Subqueries
SELECT column_name
FROM table_name
WHERE column_name = (SELECT column_name FROM table_name
WHERE condition);

1. Scaler Subquery: single row and single column.

Q- To find employees whose salary is above the average salary:

SELECT name, salary


FROM employees
WHERE salary > (SELECT AVG (salary) FROM employees);

2. Row Subquery: multiple rows and single column.

Q-To find employees who work in departments with more than 10


employees:

SELECT name

FROM employees

WHERE dept_id IN (SELECT dept_id FROM departments WHERE


employee_count > 10)

3. Table Subquery: multiple rows and multiple columns.


4. Correlated Subquery: read every row in table and comparing values
in each rows against related data.
______________________________________________________________________

WINDOW FUNCTION
Window function applies aggregate, ranking and analytical function over a
particular window (set of rows)
Over clauses is use with window function to define that window.
Select COLUMN_NAME
Fun (aggregate/ranking/analytical) over (
[ partition by clause
Order by clause
Row/range clause
])
From table_name;
OVER (): Defines the window for the function.
PARTITION BY: Divides the result set into partitions to which the window
function is applied.
ORDER BY: Specifies the order of rows within each partition.
Aggregate functions
SUM (), AVG (), MIN (), MAX(): Aggregate functions used as window
functions.
RANKING
Row number – Assigns a unique sequential integer to rows within a
partition.

Rank – rows with equal value assign the same rank and next rank is skip
Dense_rank – rows with equal value assign the same rank and no rank is
skip

ANALYTICAL / VALUE
The NTH_VALUE () function returns the value of the nth row in the window
frame.

FIRST_VALUE () and LAST_VALUE () functions return the first and last value
in the window frame, respectively.

LEAD () and LAG () functions allow you to access data from subsequent or
preceding rows, respectively.

______________________________________________________________________

SQL VIEWS
• A view in SQL table is a virtual table that represent the result of query.
• A view contains the rows and columns, just like a real table. the field
in the views from one or more real table in the database.
• Its not a physical table in the dataset but rather a store SQL query
that can be referenced like table in a subsequent query
CREATE VIEW view_name AS
SELECT columns
FROM
WHERE condition;
table_name
Modifying Views
To modify an existing view, you can use the CREATE OR REPLACE VIEW
statement:

CREATE OR REPLACE VIEW view_name AS


SELECT columns
FROM table_name
WHERE condition
table_name;
Dropping Views
To delete a view, use the DROP VIEW statement:
DROP VIEW view_name ;
______________________________________________________________________

Store procedure
A stored procedure in SQL is a set of SQL statements that can be stored in
the database and executed repeatedly.
Stored procedures can accept parameters, perform complex operations,
and return results
Its very useful as it reduce the need for rewriting SQL SUBQUIRES
Delimeter &&
CREATE PROCEDURE procedure_name
(parameter_1 data_type , parameter_2 data_type )
As
BEGIN
-- SQL statements
END
Delemeter ;

• DELIMITER &&: Temporarily changes the delimiter to && so that


MySQL can interpret the entire procedure body as a single statement
• END &&: Marks the end of the procedure definition with the custom
delimiter.
• DELIMITER; : Resets the delimiter back to the default (;).

why use SQL procedure


Using SQL stored procedures has several advantages that make them a
valuable tool in database management and application development:
1. Performance
Stored procedures are precompiled and stored in the database, which
means they are optimized for execution. This can significantly improve
performance, especially for complex queries that are executed frequently.
2. Reusability
Once a stored procedure is created, it can be reused by multiple
applications or users, reducing code duplication. This also makes
maintenance easier, as changes need to be made in only one place.

_____________________________________________________________
THE CASE STATEMENT
In SQL, a case is used to create conditional logic within a query. it allows us
to return diff value based on the specific condition.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
EXAMPLE
SELECT emp_id, name, salary,
CASE
WHEN salary <>100000 THEN 'High'
WHEN salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;

IF no condition is true it return else part


If there is no else part and no condition is true it return null.

You might also like