SlideShare una empresa de Scribd logo
1 de 34
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Sql basics
Sql  basicsSql  basics
Sql basics
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Mysql
MysqlMysql
Mysql
 
BIS05 Introduction to SQL
BIS05 Introduction to SQLBIS05 Introduction to SQL
BIS05 Introduction to SQL
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Sql operator
Sql operatorSql operator
Sql operator
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
SQL
SQLSQL
SQL
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Sql
SqlSql
Sql
 

Destacado

Managing oracle Database Instance
Managing oracle Database InstanceManaging oracle Database Instance
Managing oracle Database InstanceRam Kedem
 
SQLcl the next generation of SQLPlus?
SQLcl the next generation of SQLPlus?SQLcl the next generation of SQLPlus?
SQLcl the next generation of SQLPlus?Zohar Elkayam
 
MSSQL Server - Automation
MSSQL Server - AutomationMSSQL Server - Automation
MSSQL Server - AutomationRam Kedem
 
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic FunctionsOOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsZohar Elkayam
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Beat Signer
 
Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...
Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...
Extended ER Model and other Modelling Languages - Lecture 2 - Introduction to...Beat Signer
 
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...Beat Signer
 
eStudio 34 InAppEvent - Billy Mobile User Acquisition and Monetization
eStudio 34 InAppEvent - Billy Mobile User Acquisition and MonetizationeStudio 34 InAppEvent - Billy Mobile User Acquisition and Monetization
eStudio 34 InAppEvent - Billy Mobile User Acquisition and MonetizationWilliam Renedo
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Beat Signer
 
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)Beat Signer
 
DATA WAREHOUSING
DATA WAREHOUSINGDATA WAREHOUSING
DATA WAREHOUSINGKing Julian
 

Destacado (13)

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

Similar a Advanced SQL Webinar

Similar a Advanced SQL Webinar (20)

Sql Patterns
Sql PatternsSql Patterns
Sql Patterns
 
Obtain better data accuracy using reference tables
Obtain better data accuracy using reference tablesObtain better data accuracy using reference tables
Obtain better data accuracy using reference tables
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Introduction to sq lite
Introduction to sq liteIntroduction to sq lite
Introduction to sq lite
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
Spufi
SpufiSpufi
Spufi
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
Lecture5-SQL.docx
Lecture5-SQL.docxLecture5-SQL.docx
Lecture5-SQL.docx
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql basics
Sql basicsSql basics
Sql basics
 
SQL
SQLSQL
SQL
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorial
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 

Más de Ram Kedem

Impala use case @ edge
Impala use case @ edgeImpala use case @ edge
Impala use case @ edgeRam Kedem
 
Power Pivot and Power View
Power Pivot and Power ViewPower Pivot and Power View
Power Pivot and Power ViewRam Kedem
 
Data Mining in SSAS
Data Mining in SSASData Mining in SSAS
Data Mining in SSASRam Kedem
 
Data mining In SSAS
Data mining In SSASData mining In SSAS
Data mining In SSASRam Kedem
 
SQL Injections - Oracle
SQL Injections - OracleSQL Injections - Oracle
SQL Injections - OracleRam Kedem
 
SSAS Attributes
SSAS AttributesSSAS Attributes
SSAS AttributesRam Kedem
 
DDL Practice (Hebrew)
DDL Practice (Hebrew)DDL Practice (Hebrew)
DDL Practice (Hebrew)Ram Kedem
 
DML Practice (Hebrew)
DML Practice (Hebrew)DML Practice (Hebrew)
DML Practice (Hebrew)Ram Kedem
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Ram Kedem
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLRam Kedem
 
Introduction to Databases
Introduction to DatabasesIntroduction to Databases
Introduction to DatabasesRam Kedem
 
Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014Ram Kedem
 
Pig - Processing XML data
Pig - Processing XML dataPig - Processing XML data
Pig - Processing XML dataRam Kedem
 
SSAS Cubes & Hierarchies
SSAS Cubes & HierarchiesSSAS Cubes & Hierarchies
SSAS Cubes & HierarchiesRam Kedem
 
SSRS Basic Parameters
SSRS Basic ParametersSSRS Basic Parameters
SSRS Basic ParametersRam Kedem
 
SSRS Conditional Formatting
SSRS Conditional FormattingSSRS Conditional Formatting
SSRS Conditional FormattingRam Kedem
 
SSRS Calculated Fields
SSRS Calculated FieldsSSRS Calculated Fields
SSRS Calculated FieldsRam Kedem
 

Más de Ram Kedem (20)

Impala use case @ edge
Impala use case @ edgeImpala use case @ edge
Impala use case @ edge
 
Power Pivot and Power View
Power Pivot and Power ViewPower Pivot and Power View
Power Pivot and Power View
 
Data Mining in SSAS
Data Mining in SSASData Mining in SSAS
Data Mining in SSAS
 
Data mining In SSAS
Data mining In SSASData mining In SSAS
Data mining In SSAS
 
SQL Injections - Oracle
SQL Injections - OracleSQL Injections - Oracle
SQL Injections - Oracle
 
SSAS Attributes
SSAS AttributesSSAS Attributes
SSAS Attributes
 
SSRS Matrix
SSRS MatrixSSRS Matrix
SSRS Matrix
 
DDL Practice (Hebrew)
DDL Practice (Hebrew)DDL Practice (Hebrew)
DDL Practice (Hebrew)
 
DML Practice (Hebrew)
DML Practice (Hebrew)DML Practice (Hebrew)
DML Practice (Hebrew)
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Introduction to Databases
Introduction to DatabasesIntroduction to Databases
Introduction to Databases
 
Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014
 
Pig - Processing XML data
Pig - Processing XML dataPig - Processing XML data
Pig - Processing XML data
 
SSAS Cubes & Hierarchies
SSAS Cubes & HierarchiesSSAS Cubes & Hierarchies
SSAS Cubes & Hierarchies
 
SSRS Basic Parameters
SSRS Basic ParametersSSRS Basic Parameters
SSRS Basic Parameters
 
SSRS Gauges
SSRS GaugesSSRS Gauges
SSRS Gauges
 
SSRS Conditional Formatting
SSRS Conditional FormattingSSRS Conditional Formatting
SSRS Conditional Formatting
 
SSRS Calculated Fields
SSRS Calculated FieldsSSRS Calculated Fields
SSRS Calculated Fields
 
SSRS Groups
SSRS GroupsSSRS Groups
SSRS Groups
 

Último

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 

Último (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

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