5 Advanced DML Statements
5 Advanced DML Statements
DML Statements
FSOFT ACADEMY
02 Understand
Recognize howsubqueries in SQL
to use built-in Server
functions to perform
operations on data
1. SQL JOINS
2. Sub-Queries
SQL JOINS
Result:
Result:
Result:
ON A.Col1 = B.Col1
Result:
Result:
OUTER JOIN
CROSS JOIN
Result:
Syntax
SELECT col_names
FROM Table_A A RIGHT JOIN Table_B B ON A.Col1 = B.Col1
WHERE A.Col1 IS NULL
SUBQUERY
Exam:
SELECT SUM (Sales) AS Sale_Sum FROM Store_Information
WHERE Store_Name IN
(SELECT Store_Name FROM Geography
WHERE Region_Name = 'West');
Sale_Sum
2050
Note about specific type: Correlated subqueries (be mentioned in the next
slides)
agents table
orders table
Result:
Result:
Result:
Subqueries with IN / NOT IN: The result of a subquery introduced with IN (or with NOT
IN) is a list of zero or more values. After the subquery returns results, the outer query
makes use of them
Subqueries with EXISTS / NOT EXISTS: The subquery functions as an existence test.
Replace by CTE:
; WITH cte_Agents(agent_name, working_area, amount_of_agent)
AS
(
SELECT a.agent_name, a.working_area, COUNT(o.agent_code) AS AMOUNT_AGENT
FROM dbo.agents a INNER JOIN dbo.orders o ON A.agent_code = O.agent_code
GROUP BY a.agent_name, a.working_area
)
SELECT * FROM cte_Agents cte
WHERE cte.working_area = 'Bungnlme'
Ex:
RANKING FUNCTIONS
DENSE_RANK ROW_NUMBER
Dense_Rank
Rank