Database Querying
Tech Jam -25 november 2015
Rodger oates
Database Querying
• STUFF
• COALESCE vs ISNULL
• The LIKE Predicate
• Combining Predicates
• Joining Queries
• Table Expressions
• Window Functions
A Quick Note on Query Processing
1. FROM
2. JOINS
3. WHERE
4. GROUP BY
5. CUBE | ROLLUP
6. HAVING
7. SELECT
8. DISTINCT
9. ORDER BY
10. TOP
STUFF
REPLACE
• Used to replace parts of a string
• Will perform action through whole of original string
• Format
• REPLACE(<original>, <string to replace>, <replacement>)
• Note: <string to replace> and <replacement> do not need to be equal
in length
STUFF
• Used to insert one string into another
• Replaces characters in range specified with given string
• Format:
• STUFF(<original>, <start index>, <length>, <string to stuff>)
• Examples of use:
• www.sql-server-helper.com/
For XML PATH
• Can be used with STUFF to translate a table of results into a single
result.
• Real world example:
• Comma delimited list of names
• E.g. in names of users assigned to a given site
• See: Usage #6 in www.sql-server-helper.com
COALESCE vs ISNULL
ISNULL
• T-SQL specific
• Takes two inputs
• Format:
• ISNULL(<expression 1>, <expression 2>)
• Returns <expression 1> if not null, else <expression 2>
• Note: Data type of result is same as <expression 1> regardless of
which expression is returned
COALESCE
• Standard SQL
• Takes any number of inputs
• Format:
• COALESCE(<expression 1>, <expression 2>, …, <expression n>)
• Returns first input object that is not null
• Note: Data type of result is the same as the returned expression
The LIKE Predicate
Why LIKE?
• Used to find strings that are similar to a search term
• Without wildcards, predicate would only match with the search term
in its entirety, case insensitive
• With wildcards, we can find matches where only part of the string
matches our term
• Search terms that commence with wildcard(s) do not allow SQL to
use index ordering to speed up the query
• E.g UserFullName LIKE ‘%oates%’
Wildcards in LIKE
Source: Exam 70-461Training Kit
Combining Predicates
Types of combinations
• AND
• Predicates on both sides must be true
• OR
• Either predicate must be true
• NOT
• Negation of succeeding predicate
• Note: when predicate equates to NULL, the NOT of this predicate will still be
NULL
Natural Precedence Rules for Combinations
1. NOT
2. AND
3. OR
e.g.
WHERE col1 = ‘w’ AND col2 = ‘x’ OR col3 = ‘y’ AND col4 = ‘z’
Use of parentheses
• Can use parentheses if different precedence must be given to
predicates
• Previous example would equate to:
• WHERE (col1 = ‘w’AND col2 = ‘x’) OR (col3 = ‘y’ AND col4 = ‘z’)
• However, we could use:
• WHERE col1 = ‘w’ AND (col2 = ‘x’ OR col3 = ‘y’) AND col4 = ‘z’
Joining Queries
Joining Queries
• Types of Join
• Multi-Join Queries
• EXISTS
• Set Operators
• UNION vs UNIONALL
• INTERSECT
• EXCEPT
Types of Join
• CROSS JOIN
• Cartesian product of two tables
• Does not require a link between the tables
• INNER JOIN
• Requires a predicate between the tables (specified using the ON keyword)
• Filters out any rows that do not match the ON predicate
• SELF-JOIN
• Is simply an INNER JOIN
• Joins records in a table with other records in the same table
OUTER JOINS
• Outer joins preserve at least one side of the join if there is no match
• LEFT OUTER JOIN preserves the left table
• E.g. <tableA> LEFT OUTER JOIN <table B> preserves <table A>
• RIGHT OUTER JOIN preserves the right table
• E.g. <tableA> RIGHT OUTER JOIN <table B> preserves <table B>
• FULL OUTER JOIN preserves both tables where there is no match
• Further information on joins atTechNet
A Note on NULLs within JOINs
• Note: When determining row inclusion via predicate, a NULL value on
either side of the comparison will be discarded
• NULL is not a value, it is unknown
Multi-Join Queries
• Often need to look at multiple tables within the same query
• Use joins to link each table together
• Joins are evaluated in order of definition
• Need to be careful when combining outer joins with others, as the
results may not be what you expect
Example
Source: Exam 70-461Training Kit
Why was a supplier not returned?
Source: Exam 70-461Training Kit
How to get round this
Source: Exam 70-461Training Kit
EXISTS
• Accepts a subquery as input:
• WHERE EXISTS (SELECT … FROM <table>)
• Returns true if at least one row returned
• EXISTS doesn’t need to return the result of the subquery
• Rather, true or false depending on whether rows would have been
returned by the subquery
• Can be negated:
• WHERE NOT EXISTS (SELECT … FROM <table>)
Set Operator Guidelines
• Complete rows are matched between the input sets
• Number of columns in all queries must be the same
• Column types for corresponding columns must be compatible
• Considers two NULLs to be compatible
• This is different to JOINs
• Result column names are defined in the first query
• Result ordering can only be defined after the last input query
UNION vs UNION ALL
• UNION unifies the results of two input queries
• Both input queries must have
• UNION has an implicit DISTINCT property
• No duplicates in result
• UNION ALL returns all results, including any duplicates
UNION / UNION ALL UNION ALL
INTERSECT
• Only rows that exist in all input queries are returned
• Has an implicit DISTINCT operator
EXCEPT
• Works with two input queries
• Returns rows that exist in the first query, but not the second
Table Expressions
Types of Table Expression
1. DerivedTable
2. CommonTable Expression (CTE)
3. View
4. InlineTable-Valued Function
• 1 and 2 are only visible within the scope of the current statement and
are not reusable
• 3 and 4 have their definitions stored in the database and can be
reused
Optimisation Notes
• Table expressions are not tables, but definitions
• They do not hold any data
• They interact directly with the underlying tables
• They do not have a performance impact in themselves
• The above points must be noted when using a table expression within
a query operating on a large data set
Derived Tables
• Closely resemble a subquery
• Defined in the FROM clause of the outer query
• E.g. I want the two lowest priced products per category
Source: Exam 70-461Training Kit
Common Table Expressions (CTEs)
• Derived tables cannot be nested, if this is required, the statement
must define multiple instances of the same query
• Higher risk of mistake
• This is a CTE version of the previous query
Note: we could reuse C as many times as
we want, but only within the scope of the
current statement
Source: Exam 70-461Training Kit
A Note on Scoped Tables
• If you need to refer to the same data in multiple statements for a
given query it would be better to retrieve the data into a table.You
have two options:
• Create a temporary table
• CREATETABLE #<table name>
• Remember to use a DROPTABLE #<table name> command
• Declare a table variable
• DECLARE @<table name>TABLE
• This way, you are not having to go back to the raw tables to retrieve
the same data
Views and Inline Table-Valued Functions
• Define queries in the database, which can then be reused
• Do not store any data
• Performance issues arise from how it is used
• Views do not allow input parameters
• Functions do allow input parameters
Window Functions
Window Functions
• Window Aggregate Functions
• Window Ranking Functions
Window Aggregate Functions
• Same as group aggregate functions
• SUM,COUNT,AVG, MIN, MAX
• Are applied to a window of rows defined by the OVER clause
• Do not hide row detail
• Can mix detail and aggregated elements in the same query
• Without having to define a load of columns in the group section
Example
Window Ranking Functions
• ROW_NUMBER
• Unique number based on ORDER BY clause
• RANK
• Number based on ORDER BY clause
• Assigns same number when ordering values are tied
• Numbers may therefore not be sequential
Window Ranking Functions
• DENSE_RANK
• Similar to RANK
• Numbers are sequential
• NTILE
• Arranges rows within a partition of a number of equally sizes tiles
• Number of rows per partition is calculated by
• <total number of rows> / <partition size>
• If this calculation produces a remainder, additional row(s) are assigned to tiles in
their order defined by the ORDER BY clause (see example below)
Example
• Assuming 800 rows of data, and the following query
Source: Exam 70-461Training Kit
Example
custid orderid Val rownum rnk densernk ntile100
12 10782 12.50 1 1 1 1
27 10807 18.40 2 2 2 1
66 10586 23.80 3 3 3 1
76 10767 28.00 4 4 4 1
54 10898 30.00 5 5 5 1
88 10900 33.75 6 6 6 1
48 10883 36.00 7 7 7 1
41 11051 36.00 8 7 7 1
71 10815 40.00 9 9 8 2
38 10674 45.00 10 10 9 2
53 11057 45.00 11 10 9 2
75 10271 48.00 12 12 10 2
Source: Exam 70-461Training Kit
PARTITION
• Can be applied to window ranking functions
• Each partition has its own numbering
• E.g. to get the latest child event for each parent
Resources
• STUFF: www.sql-server-helper.com
• JOINS:TechNet
• Training:
• MicrosoftVirtual Academy
• MSDN
• Reference tutorial:
• http://www.tutorialspoint.com/sql/sql_tutorial.pdf

More Related Content

PPTX
advanced sql(database)
PPTX
SQL(database)
PPT
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
PDF
SQL JOINS
PPTX
PPTX
3. ddl create
PPTX
2. DML_INSERT_DELETE_UPDATE
advanced sql(database)
SQL(database)
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
SQL JOINS
3. ddl create
2. DML_INSERT_DELETE_UPDATE

What's hot (19)

PDF
SQL JOINS
PPTX
T sql語法之 cte 20140214
PPT
Constraints In Sql
PPTX
How oracle query works (The SQL Optimizers)
PPTX
PPTX
SQL Tutorial for Beginners
PPT
Displaying data from multiple tables
PDF
Database Management System 1
PPTX
SQL Fundamentals
PPTX
Where conditions and Operators in SQL
PPT
Sql Tutorials
PDF
Assignment 4
PPTX
Array
PDF
SQL Joins and Query Optimization
PDF
7. Using Sub Queries
PPTX
Referential integrity
PDF
Assignment 3
PPT
Database constraints
SQL JOINS
T sql語法之 cte 20140214
Constraints In Sql
How oracle query works (The SQL Optimizers)
SQL Tutorial for Beginners
Displaying data from multiple tables
Database Management System 1
SQL Fundamentals
Where conditions and Operators in SQL
Sql Tutorials
Assignment 4
Array
SQL Joins and Query Optimization
7. Using Sub Queries
Referential integrity
Assignment 3
Database constraints

Similar to Tech Jam 01 - Database Querying (20)

PPTX
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPTX
OracleSQLraining.pptx
PPT
Transact SQL (T-SQL) for Beginners (A New Hope)
PPT
Advanced Sql Training
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
PPT
Ch7
PPTX
1. dml select statement reterive data
PDF
MySQL Optimizer: What’s New in 8.0
PPTX
SQL Server Learning Drive
PPTX
SQLSERVERQUERIES.pptx
PPT
Module03
PDF
DBMS Nested & Sub Queries Set operations
PPT
Module 3 Part I - Bk1 Chapter 07.ppt
PDF
Data Engineering Interview Questions & AnswersSQL & Query Optimisation.pdf
PPTX
DDL,DML,SQL Functions and Joins
ODP
Oracle SQL Advanced
PPTX
Day-2 SQL Theory_V1.pptx
PPTX
join_SQL000000000000000000000000000000000.pptx
PDF
SQL Overview
PDF
Sql overview-1232931296681161-1
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
OracleSQLraining.pptx
Transact SQL (T-SQL) for Beginners (A New Hope)
Advanced Sql Training
More Complex SQL and Concurrency ControlModule 4.pptx
Ch7
1. dml select statement reterive data
MySQL Optimizer: What’s New in 8.0
SQL Server Learning Drive
SQLSERVERQUERIES.pptx
Module03
DBMS Nested & Sub Queries Set operations
Module 3 Part I - Bk1 Chapter 07.ppt
Data Engineering Interview Questions & AnswersSQL & Query Optimisation.pdf
DDL,DML,SQL Functions and Joins
Oracle SQL Advanced
Day-2 SQL Theory_V1.pptx
join_SQL000000000000000000000000000000000.pptx
SQL Overview
Sql overview-1232931296681161-1

Recently uploaded (20)

PDF
C language slides for c programming book by ANSI
PPTX
Hexagone difital twin solution in the desgining
PPTX
Comprehensive Guide to Digital Image Processing Concepts and Applications
PDF
Module 1 - Introduction to Generative AI.pdf
PDF
Canva Desktop App With Crack Free Download 2025?
PPTX
Presentation - Summer Internship at Samatrix.io_template_2.pptx
PDF
Difference Between Website and Web Application.pdf
PDF
10 Mistakes Agile Project Managers Still Make
PDF
Software Development Company - swapdigit | Best Mobile App Development In India
PDF
DOWNLOAD—IOBit Uninstaller Pro Crack Download Free
PDF
Coding with GPT-5- What’s New in GPT 5 That Benefits Developers.pdf
PPTX
ESDS_SAP Application Cloud Offerings.pptx
PDF
Science is Not Enough SPLC2009 Richard P. Gabriel
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
PPTX
oracle_ebs_12.2_project_cutoveroutage.pptx
PPTX
Phoenix Marketo User Group: Building Nurtures that Work for Your Audience. An...
PDF
OpenImageIO Virtual Town Hall - August 2025
PPTX
FLIGHT TICKET API | API INTEGRATION PLATFORM
PDF
Ragic Data Security Overview: Certifications, Compliance, and Network Safegua...
PDF
How to Write Automated Test Scripts Using Selenium.pdf
C language slides for c programming book by ANSI
Hexagone difital twin solution in the desgining
Comprehensive Guide to Digital Image Processing Concepts and Applications
Module 1 - Introduction to Generative AI.pdf
Canva Desktop App With Crack Free Download 2025?
Presentation - Summer Internship at Samatrix.io_template_2.pptx
Difference Between Website and Web Application.pdf
10 Mistakes Agile Project Managers Still Make
Software Development Company - swapdigit | Best Mobile App Development In India
DOWNLOAD—IOBit Uninstaller Pro Crack Download Free
Coding with GPT-5- What’s New in GPT 5 That Benefits Developers.pdf
ESDS_SAP Application Cloud Offerings.pptx
Science is Not Enough SPLC2009 Richard P. Gabriel
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
oracle_ebs_12.2_project_cutoveroutage.pptx
Phoenix Marketo User Group: Building Nurtures that Work for Your Audience. An...
OpenImageIO Virtual Town Hall - August 2025
FLIGHT TICKET API | API INTEGRATION PLATFORM
Ragic Data Security Overview: Certifications, Compliance, and Network Safegua...
How to Write Automated Test Scripts Using Selenium.pdf

Tech Jam 01 - Database Querying

  • 1. Database Querying Tech Jam -25 november 2015 Rodger oates
  • 2. Database Querying • STUFF • COALESCE vs ISNULL • The LIKE Predicate • Combining Predicates • Joining Queries • Table Expressions • Window Functions
  • 3. A Quick Note on Query Processing 1. FROM 2. JOINS 3. WHERE 4. GROUP BY 5. CUBE | ROLLUP 6. HAVING 7. SELECT 8. DISTINCT 9. ORDER BY 10. TOP
  • 5. REPLACE • Used to replace parts of a string • Will perform action through whole of original string • Format • REPLACE(<original>, <string to replace>, <replacement>) • Note: <string to replace> and <replacement> do not need to be equal in length
  • 6. STUFF • Used to insert one string into another • Replaces characters in range specified with given string • Format: • STUFF(<original>, <start index>, <length>, <string to stuff>) • Examples of use: • www.sql-server-helper.com/
  • 7. For XML PATH • Can be used with STUFF to translate a table of results into a single result. • Real world example: • Comma delimited list of names • E.g. in names of users assigned to a given site • See: Usage #6 in www.sql-server-helper.com
  • 9. ISNULL • T-SQL specific • Takes two inputs • Format: • ISNULL(<expression 1>, <expression 2>) • Returns <expression 1> if not null, else <expression 2> • Note: Data type of result is same as <expression 1> regardless of which expression is returned
  • 10. COALESCE • Standard SQL • Takes any number of inputs • Format: • COALESCE(<expression 1>, <expression 2>, …, <expression n>) • Returns first input object that is not null • Note: Data type of result is the same as the returned expression
  • 12. Why LIKE? • Used to find strings that are similar to a search term • Without wildcards, predicate would only match with the search term in its entirety, case insensitive • With wildcards, we can find matches where only part of the string matches our term • Search terms that commence with wildcard(s) do not allow SQL to use index ordering to speed up the query • E.g UserFullName LIKE ‘%oates%’
  • 13. Wildcards in LIKE Source: Exam 70-461Training Kit
  • 15. Types of combinations • AND • Predicates on both sides must be true • OR • Either predicate must be true • NOT • Negation of succeeding predicate • Note: when predicate equates to NULL, the NOT of this predicate will still be NULL
  • 16. Natural Precedence Rules for Combinations 1. NOT 2. AND 3. OR e.g. WHERE col1 = ‘w’ AND col2 = ‘x’ OR col3 = ‘y’ AND col4 = ‘z’
  • 17. Use of parentheses • Can use parentheses if different precedence must be given to predicates • Previous example would equate to: • WHERE (col1 = ‘w’AND col2 = ‘x’) OR (col3 = ‘y’ AND col4 = ‘z’) • However, we could use: • WHERE col1 = ‘w’ AND (col2 = ‘x’ OR col3 = ‘y’) AND col4 = ‘z’
  • 19. Joining Queries • Types of Join • Multi-Join Queries • EXISTS • Set Operators • UNION vs UNIONALL • INTERSECT • EXCEPT
  • 20. Types of Join • CROSS JOIN • Cartesian product of two tables • Does not require a link between the tables • INNER JOIN • Requires a predicate between the tables (specified using the ON keyword) • Filters out any rows that do not match the ON predicate • SELF-JOIN • Is simply an INNER JOIN • Joins records in a table with other records in the same table
  • 21. OUTER JOINS • Outer joins preserve at least one side of the join if there is no match • LEFT OUTER JOIN preserves the left table • E.g. <tableA> LEFT OUTER JOIN <table B> preserves <table A> • RIGHT OUTER JOIN preserves the right table • E.g. <tableA> RIGHT OUTER JOIN <table B> preserves <table B> • FULL OUTER JOIN preserves both tables where there is no match • Further information on joins atTechNet
  • 22. A Note on NULLs within JOINs • Note: When determining row inclusion via predicate, a NULL value on either side of the comparison will be discarded • NULL is not a value, it is unknown
  • 23. Multi-Join Queries • Often need to look at multiple tables within the same query • Use joins to link each table together • Joins are evaluated in order of definition • Need to be careful when combining outer joins with others, as the results may not be what you expect
  • 25. Why was a supplier not returned? Source: Exam 70-461Training Kit
  • 26. How to get round this Source: Exam 70-461Training Kit
  • 27. EXISTS • Accepts a subquery as input: • WHERE EXISTS (SELECT … FROM <table>) • Returns true if at least one row returned • EXISTS doesn’t need to return the result of the subquery • Rather, true or false depending on whether rows would have been returned by the subquery • Can be negated: • WHERE NOT EXISTS (SELECT … FROM <table>)
  • 28. Set Operator Guidelines • Complete rows are matched between the input sets • Number of columns in all queries must be the same • Column types for corresponding columns must be compatible • Considers two NULLs to be compatible • This is different to JOINs • Result column names are defined in the first query • Result ordering can only be defined after the last input query
  • 29. UNION vs UNION ALL • UNION unifies the results of two input queries • Both input queries must have • UNION has an implicit DISTINCT property • No duplicates in result • UNION ALL returns all results, including any duplicates
  • 30. UNION / UNION ALL UNION ALL
  • 31. INTERSECT • Only rows that exist in all input queries are returned • Has an implicit DISTINCT operator
  • 32. EXCEPT • Works with two input queries • Returns rows that exist in the first query, but not the second
  • 34. Types of Table Expression 1. DerivedTable 2. CommonTable Expression (CTE) 3. View 4. InlineTable-Valued Function • 1 and 2 are only visible within the scope of the current statement and are not reusable • 3 and 4 have their definitions stored in the database and can be reused
  • 35. Optimisation Notes • Table expressions are not tables, but definitions • They do not hold any data • They interact directly with the underlying tables • They do not have a performance impact in themselves • The above points must be noted when using a table expression within a query operating on a large data set
  • 36. Derived Tables • Closely resemble a subquery • Defined in the FROM clause of the outer query • E.g. I want the two lowest priced products per category Source: Exam 70-461Training Kit
  • 37. Common Table Expressions (CTEs) • Derived tables cannot be nested, if this is required, the statement must define multiple instances of the same query • Higher risk of mistake • This is a CTE version of the previous query Note: we could reuse C as many times as we want, but only within the scope of the current statement Source: Exam 70-461Training Kit
  • 38. A Note on Scoped Tables • If you need to refer to the same data in multiple statements for a given query it would be better to retrieve the data into a table.You have two options: • Create a temporary table • CREATETABLE #<table name> • Remember to use a DROPTABLE #<table name> command • Declare a table variable • DECLARE @<table name>TABLE • This way, you are not having to go back to the raw tables to retrieve the same data
  • 39. Views and Inline Table-Valued Functions • Define queries in the database, which can then be reused • Do not store any data • Performance issues arise from how it is used • Views do not allow input parameters • Functions do allow input parameters
  • 41. Window Functions • Window Aggregate Functions • Window Ranking Functions
  • 42. Window Aggregate Functions • Same as group aggregate functions • SUM,COUNT,AVG, MIN, MAX • Are applied to a window of rows defined by the OVER clause • Do not hide row detail • Can mix detail and aggregated elements in the same query • Without having to define a load of columns in the group section
  • 44. Window Ranking Functions • ROW_NUMBER • Unique number based on ORDER BY clause • RANK • Number based on ORDER BY clause • Assigns same number when ordering values are tied • Numbers may therefore not be sequential
  • 45. Window Ranking Functions • DENSE_RANK • Similar to RANK • Numbers are sequential • NTILE • Arranges rows within a partition of a number of equally sizes tiles • Number of rows per partition is calculated by • <total number of rows> / <partition size> • If this calculation produces a remainder, additional row(s) are assigned to tiles in their order defined by the ORDER BY clause (see example below)
  • 46. Example • Assuming 800 rows of data, and the following query Source: Exam 70-461Training Kit
  • 47. Example custid orderid Val rownum rnk densernk ntile100 12 10782 12.50 1 1 1 1 27 10807 18.40 2 2 2 1 66 10586 23.80 3 3 3 1 76 10767 28.00 4 4 4 1 54 10898 30.00 5 5 5 1 88 10900 33.75 6 6 6 1 48 10883 36.00 7 7 7 1 41 11051 36.00 8 7 7 1 71 10815 40.00 9 9 8 2 38 10674 45.00 10 10 9 2 53 11057 45.00 11 10 9 2 75 10271 48.00 12 12 10 2 Source: Exam 70-461Training Kit
  • 48. PARTITION • Can be applied to window ranking functions • Each partition has its own numbering • E.g. to get the latest child event for each parent
  • 49. Resources • STUFF: www.sql-server-helper.com • JOINS:TechNet • Training: • MicrosoftVirtual Academy • MSDN • Reference tutorial: • http://www.tutorialspoint.com/sql/sql_tutorial.pdf