SlideShare a Scribd company logo
Advanced SQL - Tips & Tricks
Ram kedem
About Me
• Data Team Leader at PLYmedia
• During the last decade trained thousands of people (Israel and
abroad) various topic related to the database world.
• Managing an educational website mainly focused on the SQL
language
• Owner of Ram Kedem Training
About this Book
• Gathered over a period of almost 10 years
• Includes 265 pages and 100 Different
complex SQL use-cases
• First of its kind in Israel
• Aims to provide practical knowledge based
on real user experience
• Available in Hard-Copy & Online version
Goals
• Learn real use cases of :
• Advanced Scalar Functions
• Analytic Functions
• Group Functions
• Complex Sub-Queries
• CTE and Recursive CTE
• SET Operators
• XML tricks
‫שונים‬ ‫פורומים‬ ‫המנהל‬ ‫אינטרנט‬ ‫לאתר‬,‫שונים‬ ‫משתמשים‬ ‫של‬ ‫ההודעות‬ ‫תוכן‬ ‫את‬ ‫המכילה‬ ‫טבלה‬ ‫קיימת‬.
‫אפליקצייה‬ ‫שגיאות‬ ‫בשל‬ ‫לעיתים‬,‫סימני‬ ‫נכנסים‬ ‫המלל‬ ‫בתוך‬%
‫שונות‬ ‫טכניקות‬ ‫שתי‬ ‫באמצעות‬,‫אחוז‬ ‫מכילים‬ ‫אשר‬ ‫הערכים‬ ‫את‬ ‫מצאו‬)%(
1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬
1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬
• escape_character
• Is a character that is put in front of a wildcard character to indicate
that the wildcard should be interpreted as a regular character and
not as a wildcard.
• escape_character is a character expression that has no default and
must evaluate to only one character
SELECT *
FROM percent_values
WHERE id LIKE '%^%%' ESCAPE '^'
1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬
• Using square brackets
• In MSSQL the square brackets are required if you use keywords or special
chars in the column names or identifiers.
• For example, You could name a column [First Name] (with a space)
• The [ ] in LIKE can be used to specify the % should to be interpreted as a
regular character
SELECT *
FROM percent_values
WHERE id LIKE '%[%]%'
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
‫הבאות‬ ‫הטבלאות‬ ‫נתונות‬:
‫לקוחות‬ ‫טבלת‬,‫מס‬ ‫לגבי‬ ‫תיאור‬ ‫למצוא‬ ‫ניתן‬ ‫שורה‬ ‫בכל‬'‫הלקוח‬,
‫שמו‬,‫ומס‬'‫מתגורר‬ ‫הוא‬ ‫בה‬ ‫העיר‬
‫ערים‬ ‫טבלת‬,‫עבור‬ ‫הערים‬ ‫שמות‬ ‫את‬ ‫למצוא‬ ‫ניתן‬ ‫זו‬ ‫בטבלה‬
‫הלקוחות‬ ‫טבלת‬
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
‫העיר‬ ‫את‬ ‫תציג‬ ‫שורה‬ ‫בכל‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬,‫בה‬ ‫מתגוררים‬ ‫אשר‬ ‫השונים‬ ‫הלקוחות‬ ‫ואת‬
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
STUFF() Function
• The STUFF function deletes a sequence of characters from a source string
and then inserts another sequence of characters into the source string,
starting at a specified position.
SELECT STUFF('Hello' , 1,1,'B') => Bellow
SELECT STUFF('Hello' , 1,2,'B') => Bllo
SELECT STUFF('Hello', 2, 2, '**WORLD**') => H**WORLD**lo
SELECT STUFF('Hello', 1, 1, '') => ello
Correlated Sub Queries VS. Simple Sub Queries
• Correlated Subquery is a sub-query that uses values from the outer
query. the inner query has to be executed for every row of outer
query
• Simple subquery doesn't use values from the outer query and is being
calculated only once
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
Simple Sub Query
SELECT customerName, CityID
FROM customers
WHERE CityID = (SELECT cityID FROM cities WHERE CityName = 'Tel Aviv')
• Inner – what’s the cityID of Tel Aviv ? (only once)
• Outer – Display the customerName and CityID for customers whose
CityID equals to the output of inner query
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
Correlated Sub Query
SELECT CUST.CustomerName,
(SELECT CityName FROM Cities CT WHERE CT.CityID = CUST.CityID)
FROM customers CUST
• Inner – what’s the cityName for the FIRST row in the table ?
• Outer – Display the customerName in the FIRST row
• Inner – what’s the cityName for the SECOND row in the table ?
• Outer – Display the customerName in the SECOND row
…
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
For XML Path
1. Get XML element string with FOR XML
• Adding FOR XML PATH to the end of a query allows you to output the
results of the query as XML elements, with the element name
contained in the PATH argument
SELECT ',' + CustomerName
FROM Customers
FOR XML PATH ('')
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
For XML Path
2. Remove leading comma with STUFF
• The STUFF statement literally "stuffs” one string into another, replacing
characters within the first string. We, however, are using it simply to
remove the first character of the resultant list of values.
SELECT STUFF
(
(SELECT ',' + CustomerName
FROM Customers
FOR XML PATH ('')) ,1,1,''
)
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
Putting it all together
SELECT CityID, CityName,
STUFF
(
(SELECT ',' + CustomerName
FROM Customers InCust
WHERE InCust.CityID = CT.CityID
FOR XML PATH ('')) ,1,1,''
) AS 'Customers'
FROM Cities CT
2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
‫אות‬ ‫לפי‬ ‫מקובצות‬ ‫מסויים‬ ‫במשפט‬ ‫האותיות‬ ‫כמות‬ ‫את‬ ‫מחזירה‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬,‫צמד‬ ‫עבור‬ ‫לדוגמא‬
‫המילים‬:
‫הבאה‬ ‫התוצאה‬ ‫תתקבל‬
3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
CTE Basics
• A common table expression (CTE) can be thought of as a temporary
result set that is defined within the execution scope of a single
SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement
• Using a CTE offers the advantages of improved readability and ease in
maintenance of complex queries.
• The query can be divided into separate, simple, logical building
blocks. These simple blocks can then be used to build more complex,
interim CTEs until the final result set is generated.
3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
;WITH [CountCustomers] (col1, col2) AS
(
SELECT COUNT(*) CNT , CityID
FROM customers
GROUP BY CityID
)
SELECT *
FROM [CountCustomers]
3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
CTE name Column names
CTE name
Actual Query
;WITH "Numbers" (id) AS
(
SELECT 1
UNION ALL
SELECT id + 1 FROM "Numbers"
WHERE id < 20
)
SELECT *
FROM "Numbers"
3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
CTE name Column name
CTE name
Anchor query (runs once and the results ‘seed’ the
Recursive query)
Recursive query (runs multiple times and
is the criteria for the remaining results)
UNION ALL statement to bind the Anchor and
Recursive queries together.
WITH "wordlen"(id) AS (
SELECT 1
UNION ALL
SELECT id + 1 FROM "wordlen"
WHERE id < (SELECT LEN('hello world'))
)
SELECT letter, COUNT(letter)
FROM
(
SELECT SUBSTRING('hello world' , id , 1) letter
FROM "wordlen"
WHERE SUBSTRING('hello world' , id , 1) <> ' '
) in_tab
GROUP BY letter
ORDER BY letter
3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
‫שונים‬ ‫יוזרים‬ ‫של‬ ‫המלאים‬ ‫שמותיהם‬ ‫את‬ ‫המכילה‬ ‫טבלה‬ ‫נתונה‬(‫פרטי‬ ‫שם‬,‫אמצעי‬ ‫שם‬,‫משפחה‬ ‫ושם‬.)‫ליוזרים‬
‫אמצעי‬ ‫שם‬ ‫קיים‬ ‫לא‬ ‫מסויימים‬,‫בלבד‬ ‫משפחתו‬ ‫ושם‬ ‫הפרטי‬ ‫שמו‬ ‫יופיעו‬ ‫אלו‬ ‫במקרים‬.‫את‬ ‫עדכן‬ ‫לא‬ ‫יוזר‬ ‫לעיתים‬
‫משפחתו‬ ‫שם‬ ‫ואת‬ ‫האמצעי‬ ‫שמו‬,‫בלבד‬ ‫הפרטי‬ ‫שמו‬ ‫יופיע‬ ‫אלו‬ ‫במקרים‬.
4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
‫הפרטי‬ ‫שמו‬ ‫את‬ ‫שונות‬ ‫בעמודות‬ ‫המציגה‬ ‫שאילה‬ ‫כתבו‬,‫האמצעי‬ ‫שמו‬,‫מהיוזרים‬ ‫אחד‬ ‫כל‬ ‫של‬ ‫משפחתו‬ ‫ושם‬.
‫האמצעי‬ ‫והשם‬ ‫במידה‬‫מופיע‬ ‫לא‬ ‫המשפחה‬ ‫שם‬,‫יוצג‬NULL.
4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
REVERSE ()
• Returns the reverse order of a string value.
• SELECT REVERSE('Hello') => 'Olleh'
REPLACE ()
• Replaces all occurrences of a specified string value with another string value.
• SELECT REPLACE('Hello World','World','SQL') => 'Hello SQL'
PARSENAME ()
• Returns the specified part of an object name
• SELECT PARSENAME('Word1.Word2.Word3.Word4',1) => 'Word4'
4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
SELECT REVERSE('Hello,World')
SELECT REPLACE(REVERSE('Hello,World'),',','.')
SELECT PARSENAME(REPLACE(REVERSE('Hello,World'),',','.'),1)
SELECT REVERSE(PARSENAME(REPLACE(REVERSE('Hello,World'),',','.'),1))
4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
dlroW,olleH
dlroW.olleH
olleH
Hello
SELECT FirstName,
CASE WHEN lastName IS NULL THEN NULL ELSE MiddleName END AS MiddleName,
CASE WHEN LastName IS NULL THEN MiddleName ELSE LastName END AS LastName
FROM
(
SELECT
Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 1)) As FirstName
, Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 2)) As MiddleName
, Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 3)) As LastName
FROM users
) IN_TAB
4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
If there is no value in lastName then middleName is actually NULL
If there is no value in lastName then middleName is actually lastName
putting it all together
‫הבאה‬ ‫הטבלה‬ ‫נתונה‬.‫במערכת‬ ‫תקלה‬ ‫התרחשה‬ ‫בהם‬ ‫תאריכים‬ ‫תיעוד‬ ‫למצוא‬ ‫ניתן‬ ‫זו‬ ‫בטבלה‬.‫כי‬ ‫לראות‬ ‫ניתן‬
‫שגיאה‬ ‫בין‬1‫מס‬ ‫לשגיאה‬'2‫יומיים‬ ‫עברו‬,‫שגיאה‬ ‫בין‬2‫לשגיאה‬3‫עברו‬6‫ימים‬,‫הלאה‬ ‫וכן‬
‫לשגיאה‬ ‫שגיאה‬ ‫בין‬ ‫הממוצע‬ ‫הימים‬ ‫מספר‬ ‫את‬ ‫מחזירה‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬
5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬
5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬
LEAD() Function
• LEAD provides access to a row at a given physical offset that follows
the current row. Use this analytic function in a SELECT statement to
compare values in the current row with values in a following row
Basic Usage
• LEAD(column_name) OVER (ORDER BY column_name)
5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬
Putting it all together
SELECT AVG(DIFF) AVG_DIFF
FROM
(
SELECT id,
fdate,
ISNULL(DATEDIFF(day, fdate, LEAD(fdate)
OVER (ORDER BY id)),0) DIFF
FROM FailuresLog
) IN_TAB
‫הבאה‬ ‫הצבעים‬ ‫טבלת‬ ‫נתונה‬,‫זוגי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫מופיעים‬ ‫הלבנים‬ ‫הצבעים‬ ‫כי‬ ‫לראות‬ ‫ניתן‬,‫והצבעים‬
‫אי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫מופיעים‬ ‫השחורים‬-‫זוגי‬.
‫באמצעות‬6‫שונות‬ ‫טכניקות‬,‫הצבעים‬ ‫בין‬ ‫המחליפה‬ ‫שאילתה‬ ‫כתבו‬(‫מספר‬ ‫תחת‬ ‫יופיעו‬ ‫השחורים‬ ‫הצבעים‬
‫זוגי‬ ‫סידורי‬,‫אי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫יופיעו‬ ‫הלבנים‬ ‫והצבעים‬-‫זוגי‬)
6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬
6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬
-- 1. CASE
SELECT cid, CASE WHEN cdesc = 'black' THEN 'white' ELSE 'black' END cdesc
FROM colors
-- 2. REPLACE
SELECT cid, REPLACE (cdesc, 'black', 'white')
FROM colors
WHERE cdesc = 'black'
UNION ALL
SELECT cid, REPLACE (cdesc, 'white', 'black')
FROM colors
WHERE cdesc = 'white'
ORDER BY 1
-- 3. IIF
SELECT IIF(cdesc = 'white' , 'black' , 'white')
FROM colors
6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬
-- 4. SUBQUERY
SELECT cid, (SELECT DISTINCT cdesc FROM colors in_tbl WHERE cdesc <> out_tbl.cdesc)
FROM colors out_tbl
-- 5. JOIN
SELECT c1.cid, c2.cdesc
FROM colors c1 JOIN (SELECT DISTINCT cdesc FROM colors) c2
ON c1.cdesc <> c2.cdesc
ORDER BY 1
-- 6. ISNULL / NULLIF
SELECT cid, ISNULL(NULLIF('white', cdesc), 'black')
FROM colors
Advanced SQL Course
• Will take place between March 21 and 28 at John-Bryce Tel-Aviv
• 3 Meetings, 17:30-21:00
• More details can be found here
Thanks ! keep in touch
Ram Kedem
ram.kdm@gmail.com
Facebook
Linkedin
ramkedem.com

More Related Content

PPT
Advanced Sql Training
PPTX
STRUCTURE OF SQL QUERIES
PPT
SQL Tutorial - Basic Commands
PPTX
introdution to SQL and SQL functions
PPT
Introduction to-sql
PPT
Chapter 07 ddl_sql
PPTX
Sql.pptx
PPTX
SQL Basics
Advanced Sql Training
STRUCTURE OF SQL QUERIES
SQL Tutorial - Basic Commands
introdution to SQL and SQL functions
Introduction to-sql
Chapter 07 ddl_sql
Sql.pptx
SQL Basics

What's hot (20)

PPTX
Sql basics
PPTX
Creating database using sql commands
PPTX
SQL - Structured query language introduction
PPT
Mysql
ODP
BIS05 Introduction to SQL
DOC
A must Sql notes for beginners
PPTX
Sql operator
PPT
SQL Queries
PPTX
SQL commands
PDF
Chapter8 my sql revision tour
PPTX
SQL Queries Information
DOC
PPTX
Introduction to SQL
PPTX
DDL,DML,SQL Functions and Joins
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PDF
Assignment#07
PPTX
SQL Server Learning Drive
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
PDF
Sql basics
Creating database using sql commands
SQL - Structured query language introduction
Mysql
BIS05 Introduction to SQL
A must Sql notes for beginners
Sql operator
SQL Queries
SQL commands
Chapter8 my sql revision tour
SQL Queries Information
Introduction to SQL
DDL,DML,SQL Functions and Joins
Database Systems - SQL - DDL Statements (Chapter 3/3)
Assignment#07
SQL Server Learning Drive
Database Management - Lecture 2 - SQL select, insert, update and delete
Ad

Viewers also liked (13)

PPTX
Managing oracle Database Instance
PDF
SQLcl the next generation of SQLPlus?
PDF
MSSQL Server - Automation
PDF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPT
Advanced sql
PDF
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
PDF
Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...
PDF
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
PPTX
eStudio 34 InAppEvent - Billy Mobile User Acquisition and Monetization
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
PDF
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
PPTX
DATA WAREHOUSING
Managing oracle Database Instance
SQLcl the next generation of SQLPlus?
MSSQL Server - Automation
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Advanced sql
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
eStudio 34 InAppEvent - Billy Mobile User Acquisition and Monetization
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
DATA WAREHOUSING
Ad

Similar to Advanced SQL Webinar (20)

PDF
DP080_Lecture_2 SQL related document.pdf
PPT
Sql Patterns
PPTX
Obtain better data accuracy using reference tables
PPT
Sql 2006
PPT
Db1 lecture4
PDF
Introduction to sq lite
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PPTX
Presentation on SQL Basics to Advance in DBMS
PPTX
PPT
lecture2.ppt
PPT
lecture2.ppt
PDF
SQL Overview
PDF
R_CheatSheet.pdf
DOCX
Lecture5-SQL.docx
PPT
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
PPTX
ADVANCE ITT BY PRASAD
PDF
SQL Basics
PDF
Sql basics
PDF
Sql General
PDF
DP080_Lecture_2 SQL related document.pdf
Sql Patterns
Obtain better data accuracy using reference tables
Sql 2006
Db1 lecture4
Introduction to sq lite
Chapter – 6 SQL Lab Tutorial.pdf
Presentation on SQL Basics to Advance in DBMS
lecture2.ppt
lecture2.ppt
SQL Overview
R_CheatSheet.pdf
Lecture5-SQL.docx
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
ADVANCE ITT BY PRASAD
SQL Basics
Sql basics
Sql General

More from Ram Kedem (20)

PDF
Impala use case @ edge
PDF
Power Pivot and Power View
PDF
Data Mining in SSAS
PDF
Data mining In SSAS
PPTX
SQL Injections - Oracle
PDF
SSAS Attributes
PDF
SSRS Matrix
PDF
DDL Practice (Hebrew)
PDF
DML Practice (Hebrew)
PDF
Exploring Oracle Database Architecture (Hebrew)
PDF
Introduction to SQL
PDF
Introduction to Databases
PDF
Deploy SSRS Project - SQL Server 2014
PDF
Pig - Processing XML data
PDF
SSAS Cubes & Hierarchies
PDF
SSRS Basic Parameters
PPTX
SSRS Gauges
PDF
SSRS Conditional Formatting
PDF
SSRS Calculated Fields
PDF
SSRS Groups
Impala use case @ edge
Power Pivot and Power View
Data Mining in SSAS
Data mining In SSAS
SQL Injections - Oracle
SSAS Attributes
SSRS Matrix
DDL Practice (Hebrew)
DML Practice (Hebrew)
Exploring Oracle Database Architecture (Hebrew)
Introduction to SQL
Introduction to Databases
Deploy SSRS Project - SQL Server 2014
Pig - Processing XML data
SSAS Cubes & Hierarchies
SSRS Basic Parameters
SSRS Gauges
SSRS Conditional Formatting
SSRS Calculated Fields
SSRS Groups

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Lesson notes of climatology university.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Institutional Correction lecture only . . .
PPTX
master seminar digital applications in india
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
Lesson notes of climatology university.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Insiders guide to clinical Medicine.pdf
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
TR - Agricultural Crops Production NC III.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Institutional Correction lecture only . . .
master seminar digital applications in india
PPH.pptx obstetrics and gynecology in nursing
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Renaissance Architecture: A Journey from Faith to Humanism
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

Advanced SQL Webinar

  • 1. Advanced SQL - Tips & Tricks Ram kedem
  • 2. About Me • Data Team Leader at PLYmedia • During the last decade trained thousands of people (Israel and abroad) various topic related to the database world. • Managing an educational website mainly focused on the SQL language • Owner of Ram Kedem Training
  • 3. About this Book • Gathered over a period of almost 10 years • Includes 265 pages and 100 Different complex SQL use-cases • First of its kind in Israel • Aims to provide practical knowledge based on real user experience • Available in Hard-Copy & Online version
  • 4. Goals • Learn real use cases of : • Advanced Scalar Functions • Analytic Functions • Group Functions • Complex Sub-Queries • CTE and Recursive CTE • SET Operators • XML tricks
  • 5. ‫שונים‬ ‫פורומים‬ ‫המנהל‬ ‫אינטרנט‬ ‫לאתר‬,‫שונים‬ ‫משתמשים‬ ‫של‬ ‫ההודעות‬ ‫תוכן‬ ‫את‬ ‫המכילה‬ ‫טבלה‬ ‫קיימת‬. ‫אפליקצייה‬ ‫שגיאות‬ ‫בשל‬ ‫לעיתים‬,‫סימני‬ ‫נכנסים‬ ‫המלל‬ ‫בתוך‬% ‫שונות‬ ‫טכניקות‬ ‫שתי‬ ‫באמצעות‬,‫אחוז‬ ‫מכילים‬ ‫אשר‬ ‫הערכים‬ ‫את‬ ‫מצאו‬)%( 1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬
  • 6. 1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬ • escape_character • Is a character that is put in front of a wildcard character to indicate that the wildcard should be interpreted as a regular character and not as a wildcard. • escape_character is a character expression that has no default and must evaluate to only one character SELECT * FROM percent_values WHERE id LIKE '%^%%' ESCAPE '^'
  • 7. 1.‫אחוז‬ ‫עם‬ ‫ערכים‬ ‫מציאת‬ • Using square brackets • In MSSQL the square brackets are required if you use keywords or special chars in the column names or identifiers. • For example, You could name a column [First Name] (with a space) • The [ ] in LIKE can be used to specify the % should to be interpreted as a regular character SELECT * FROM percent_values WHERE id LIKE '%[%]%'
  • 8. 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬ ‫הבאות‬ ‫הטבלאות‬ ‫נתונות‬: ‫לקוחות‬ ‫טבלת‬,‫מס‬ ‫לגבי‬ ‫תיאור‬ ‫למצוא‬ ‫ניתן‬ ‫שורה‬ ‫בכל‬'‫הלקוח‬, ‫שמו‬,‫ומס‬'‫מתגורר‬ ‫הוא‬ ‫בה‬ ‫העיר‬ ‫ערים‬ ‫טבלת‬,‫עבור‬ ‫הערים‬ ‫שמות‬ ‫את‬ ‫למצוא‬ ‫ניתן‬ ‫זו‬ ‫בטבלה‬ ‫הלקוחות‬ ‫טבלת‬
  • 9. 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬ ‫העיר‬ ‫את‬ ‫תציג‬ ‫שורה‬ ‫בכל‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬,‫בה‬ ‫מתגוררים‬ ‫אשר‬ ‫השונים‬ ‫הלקוחות‬ ‫ואת‬
  • 10. 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬ STUFF() Function • The STUFF function deletes a sequence of characters from a source string and then inserts another sequence of characters into the source string, starting at a specified position. SELECT STUFF('Hello' , 1,1,'B') => Bellow SELECT STUFF('Hello' , 1,2,'B') => Bllo SELECT STUFF('Hello', 2, 2, '**WORLD**') => H**WORLD**lo SELECT STUFF('Hello', 1, 1, '') => ello
  • 11. Correlated Sub Queries VS. Simple Sub Queries • Correlated Subquery is a sub-query that uses values from the outer query. the inner query has to be executed for every row of outer query • Simple subquery doesn't use values from the outer query and is being calculated only once 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 12. Simple Sub Query SELECT customerName, CityID FROM customers WHERE CityID = (SELECT cityID FROM cities WHERE CityName = 'Tel Aviv') • Inner – what’s the cityID of Tel Aviv ? (only once) • Outer – Display the customerName and CityID for customers whose CityID equals to the output of inner query 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 13. Correlated Sub Query SELECT CUST.CustomerName, (SELECT CityName FROM Cities CT WHERE CT.CityID = CUST.CityID) FROM customers CUST • Inner – what’s the cityName for the FIRST row in the table ? • Outer – Display the customerName in the FIRST row • Inner – what’s the cityName for the SECOND row in the table ? • Outer – Display the customerName in the SECOND row … 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 14. For XML Path 1. Get XML element string with FOR XML • Adding FOR XML PATH to the end of a query allows you to output the results of the query as XML elements, with the element name contained in the PATH argument SELECT ',' + CustomerName FROM Customers FOR XML PATH ('') 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 15. For XML Path 2. Remove leading comma with STUFF • The STUFF statement literally "stuffs” one string into another, replacing characters within the first string. We, however, are using it simply to remove the first character of the resultant list of values. SELECT STUFF ( (SELECT ',' + CustomerName FROM Customers FOR XML PATH ('')) ,1,1,'' ) 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 16. Putting it all together SELECT CityID, CityName, STUFF ( (SELECT ',' + CustomerName FROM Customers InCust WHERE InCust.CityID = CT.CityID FOR XML PATH ('')) ,1,1,'' ) AS 'Customers' FROM Cities CT 2.‫אחת‬ ‫שורה‬ ‫לתוך‬ ‫ערכים‬ ‫איחוד‬
  • 17. ‫אות‬ ‫לפי‬ ‫מקובצות‬ ‫מסויים‬ ‫במשפט‬ ‫האותיות‬ ‫כמות‬ ‫את‬ ‫מחזירה‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬,‫צמד‬ ‫עבור‬ ‫לדוגמא‬ ‫המילים‬: ‫הבאה‬ ‫התוצאה‬ ‫תתקבל‬ 3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
  • 18. CTE Basics • A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement • Using a CTE offers the advantages of improved readability and ease in maintenance of complex queries. • The query can be divided into separate, simple, logical building blocks. These simple blocks can then be used to build more complex, interim CTEs until the final result set is generated. 3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
  • 19. ;WITH [CountCustomers] (col1, col2) AS ( SELECT COUNT(*) CNT , CityID FROM customers GROUP BY CityID ) SELECT * FROM [CountCustomers] 3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬ CTE name Column names CTE name Actual Query
  • 20. ;WITH "Numbers" (id) AS ( SELECT 1 UNION ALL SELECT id + 1 FROM "Numbers" WHERE id < 20 ) SELECT * FROM "Numbers" 3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬ CTE name Column name CTE name Anchor query (runs once and the results ‘seed’ the Recursive query) Recursive query (runs multiple times and is the criteria for the remaining results) UNION ALL statement to bind the Anchor and Recursive queries together.
  • 21. WITH "wordlen"(id) AS ( SELECT 1 UNION ALL SELECT id + 1 FROM "wordlen" WHERE id < (SELECT LEN('hello world')) ) SELECT letter, COUNT(letter) FROM ( SELECT SUBSTRING('hello world' , id , 1) letter FROM "wordlen" WHERE SUBSTRING('hello world' , id , 1) <> ' ' ) in_tab GROUP BY letter ORDER BY letter 3.‫במשפט‬ ‫תווים‬ ‫כמות‬ ‫הצגת‬
  • 22. ‫שונים‬ ‫יוזרים‬ ‫של‬ ‫המלאים‬ ‫שמותיהם‬ ‫את‬ ‫המכילה‬ ‫טבלה‬ ‫נתונה‬(‫פרטי‬ ‫שם‬,‫אמצעי‬ ‫שם‬,‫משפחה‬ ‫ושם‬.)‫ליוזרים‬ ‫אמצעי‬ ‫שם‬ ‫קיים‬ ‫לא‬ ‫מסויימים‬,‫בלבד‬ ‫משפחתו‬ ‫ושם‬ ‫הפרטי‬ ‫שמו‬ ‫יופיעו‬ ‫אלו‬ ‫במקרים‬.‫את‬ ‫עדכן‬ ‫לא‬ ‫יוזר‬ ‫לעיתים‬ ‫משפחתו‬ ‫שם‬ ‫ואת‬ ‫האמצעי‬ ‫שמו‬,‫בלבד‬ ‫הפרטי‬ ‫שמו‬ ‫יופיע‬ ‫אלו‬ ‫במקרים‬. 4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
  • 23. ‫הפרטי‬ ‫שמו‬ ‫את‬ ‫שונות‬ ‫בעמודות‬ ‫המציגה‬ ‫שאילה‬ ‫כתבו‬,‫האמצעי‬ ‫שמו‬,‫מהיוזרים‬ ‫אחד‬ ‫כל‬ ‫של‬ ‫משפחתו‬ ‫ושם‬. ‫האמצעי‬ ‫והשם‬ ‫במידה‬‫מופיע‬ ‫לא‬ ‫המשפחה‬ ‫שם‬,‫יוצג‬NULL. 4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
  • 24. REVERSE () • Returns the reverse order of a string value. • SELECT REVERSE('Hello') => 'Olleh' REPLACE () • Replaces all occurrences of a specified string value with another string value. • SELECT REPLACE('Hello World','World','SQL') => 'Hello SQL' PARSENAME () • Returns the specified part of an object name • SELECT PARSENAME('Word1.Word2.Word3.Word4',1) => 'Word4' 4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬
  • 25. SELECT REVERSE('Hello,World') SELECT REPLACE(REVERSE('Hello,World'),',','.') SELECT PARSENAME(REPLACE(REVERSE('Hello,World'),',','.'),1) SELECT REVERSE(PARSENAME(REPLACE(REVERSE('Hello,World'),',','.'),1)) 4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬ dlroW,olleH dlroW.olleH olleH Hello
  • 26. SELECT FirstName, CASE WHEN lastName IS NULL THEN NULL ELSE MiddleName END AS MiddleName, CASE WHEN LastName IS NULL THEN MiddleName ELSE LastName END AS LastName FROM ( SELECT Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 1)) As FirstName , Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 2)) As MiddleName , Reverse(ParseName(Replace(Reverse(username), ' ', '.'), 3)) As LastName FROM users ) IN_TAB 4.‫בעמודה‬ ‫מילים‬ ‫פיצול‬ If there is no value in lastName then middleName is actually NULL If there is no value in lastName then middleName is actually lastName putting it all together
  • 27. ‫הבאה‬ ‫הטבלה‬ ‫נתונה‬.‫במערכת‬ ‫תקלה‬ ‫התרחשה‬ ‫בהם‬ ‫תאריכים‬ ‫תיעוד‬ ‫למצוא‬ ‫ניתן‬ ‫זו‬ ‫בטבלה‬.‫כי‬ ‫לראות‬ ‫ניתן‬ ‫שגיאה‬ ‫בין‬1‫מס‬ ‫לשגיאה‬'2‫יומיים‬ ‫עברו‬,‫שגיאה‬ ‫בין‬2‫לשגיאה‬3‫עברו‬6‫ימים‬,‫הלאה‬ ‫וכן‬ ‫לשגיאה‬ ‫שגיאה‬ ‫בין‬ ‫הממוצע‬ ‫הימים‬ ‫מספר‬ ‫את‬ ‫מחזירה‬ ‫אשר‬ ‫שאילתה‬ ‫כתבו‬ 5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬
  • 28. 5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬ LEAD() Function • LEAD provides access to a row at a given physical offset that follows the current row. Use this analytic function in a SELECT statement to compare values in the current row with values in a following row Basic Usage • LEAD(column_name) OVER (ORDER BY column_name)
  • 29. 5.‫ממוצעים‬ ‫זמנים‬ ‫חישוב‬ Putting it all together SELECT AVG(DIFF) AVG_DIFF FROM ( SELECT id, fdate, ISNULL(DATEDIFF(day, fdate, LEAD(fdate) OVER (ORDER BY id)),0) DIFF FROM FailuresLog ) IN_TAB
  • 30. ‫הבאה‬ ‫הצבעים‬ ‫טבלת‬ ‫נתונה‬,‫זוגי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫מופיעים‬ ‫הלבנים‬ ‫הצבעים‬ ‫כי‬ ‫לראות‬ ‫ניתן‬,‫והצבעים‬ ‫אי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫מופיעים‬ ‫השחורים‬-‫זוגי‬. ‫באמצעות‬6‫שונות‬ ‫טכניקות‬,‫הצבעים‬ ‫בין‬ ‫המחליפה‬ ‫שאילתה‬ ‫כתבו‬(‫מספר‬ ‫תחת‬ ‫יופיעו‬ ‫השחורים‬ ‫הצבעים‬ ‫זוגי‬ ‫סידורי‬,‫אי‬ ‫סידורי‬ ‫מספר‬ ‫תחת‬ ‫יופיעו‬ ‫הלבנים‬ ‫והצבעים‬-‫זוגי‬) 6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬
  • 31. 6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬ -- 1. CASE SELECT cid, CASE WHEN cdesc = 'black' THEN 'white' ELSE 'black' END cdesc FROM colors -- 2. REPLACE SELECT cid, REPLACE (cdesc, 'black', 'white') FROM colors WHERE cdesc = 'black' UNION ALL SELECT cid, REPLACE (cdesc, 'white', 'black') FROM colors WHERE cdesc = 'white' ORDER BY 1 -- 3. IIF SELECT IIF(cdesc = 'white' , 'black' , 'white') FROM colors
  • 32. 6.‫מותנית‬ ‫להחלפה‬ ‫מתודות‬ -- 4. SUBQUERY SELECT cid, (SELECT DISTINCT cdesc FROM colors in_tbl WHERE cdesc <> out_tbl.cdesc) FROM colors out_tbl -- 5. JOIN SELECT c1.cid, c2.cdesc FROM colors c1 JOIN (SELECT DISTINCT cdesc FROM colors) c2 ON c1.cdesc <> c2.cdesc ORDER BY 1 -- 6. ISNULL / NULLIF SELECT cid, ISNULL(NULLIF('white', cdesc), 'black') FROM colors
  • 33. Advanced SQL Course • Will take place between March 21 and 28 at John-Bryce Tel-Aviv • 3 Meetings, 17:30-21:00 • More details can be found here
  • 34. Thanks ! keep in touch Ram Kedem ram.kdm@gmail.com Facebook Linkedin ramkedem.com