Create Transact-SQL SELECT queries Flashcards
Ways to assign alias in SELECT statement
- column AS alias
- column alias
- alias = column
When do you need to delimit identifiers?
When it doesn’t comply with the rules:
- First character must be a letter, underscore, @, or #.
- Subsequent characters can include letters, decimal numbers, @, #, $, or underscore.
- Cannot be a reserved keyword in T-SQL
- Cannot have embedded spaces
- Must not include supplementary characters
Two ways to delimit identifiers
- “2017”
2. [2017]
Why shouldn’t you use = when looking for a NULL? What should you use instead?
Because nothing is considered equal to a NULL–not even another NULL.
You should use IS NULL or IS NOT NULL operators
e.g.
WHERE region IS NULL;
What is the order of operations for AND, OR, and NOT?
NOT, AND, OR
Note: can use parentheses to specify order
How should you identify Unicode character strings?
N’string’
Syntax for pattern matching
What do these wildcards mean?
%, _, [characters], [character-range], [^characters]
column LIKE pattern
% : matches any string including empty one
_ : a single character
[characters] : a single character from a list of characters
[character-range] : a single character from a range
[^characters] : a single character that is not in the list or range
How to look for a character that is considered a wildcard?
- Use escape character: col1 LIKE ‘!_%’ ESCAPE ‘!’ looks for strings that start with an underscore
- Use square brackets: col1 LIKE ‘[_]%’
Which date form is considered language-neutral for all date and time types?
‘YYYYMMDD’
You’re trying to find orders placed in April. What is wrong with the following and what should you do instead?
SELECT orderid, orderdate, empid
FROM Sales.Orders2
WHERE orderdate BETWEEN ‘20160401’ AND ‘20160430 23:59:59.999’
The value is rounded up to the next millisecond. This also returns orders placed in May 1, 2016.
SELECT orderid, orderdate, empid
FROM Sales.Orders2
WHERE orderdate >= ‘20160401’ AND orderdate < ‘20160501’
Can you order by columns that are not in the SELECT statement?
When would this fail?
Yes
It can fail when DISTINCT is used because since duplicates are removed, result rows don’t necessarily map to source rows in a one-to-one manner
In what order does SQL process the components of a query?
FROM WHERE GROUP BY HAVING SELECT ORDER BY
Syntax for TOP for both number of rows and percent
Does TOP PERCENT round up or down the number of rows?
SELECT TOP (4) col1, col2
FROM Sales.Orders
ORDER BY orderdate DESC;
Note: Can also specify number of rows without parentheses, but correct syntax is with
Note: Can use expressions as inputs
SELECT TOP (4) PERCENT col1, col2
FROM Sales.Orders
ORDER BY orderdate DESC;
Note: TOP PERCENT rounds up for number of rows
What does OFFSET do?
Syntax for OFFSET-FETCH
Which clause is required for OFFSET-FETCH?
Do you always need to use OFFSET-FETCH together?
What if want to filter a certain number of rows in arbitrary order?
After which statement is OFFSET-FETCH processed in a query?
It skips rows
SELECT col1, col2
FROM Sales.Orders
ORDER BY orderdate
OFFSET 50 ROWS FETCH NEXT 25 ROWS ONLY;
ORDER BY is required to use OFFSET-FETCH
FETCH clause required OFFSET, but OFFSET doesn’t require FETCH
Note: If skipping 0 rows, use FETCH FIRST instead of NEXT
Note: can use expressions as inputs
For arbitrary order use ORDER BY (SELECT NULL)
OFFSET-FETCH is processed after SELECT. Think of it as an extension of the ORDER BY clause
Between TOP and OFFSET-FETCH, which one is standard and which one is not?
OFFSET-FETCH is standard
Guidelines for set operators: UNION, UNION ALL, INTERSECT, and EXCEPT
- About number of columns and column types
- Set operators use distinctness-based comparison rather than equality-based. What does this mean?
- These operators are set operators and not cursor operators. What restriction does this impose on the individual queries?
- Where can you have an ORDER BY clause?
- How are column names of the result columns determined?
- Number of columns between the queries has to be the same and the column types of corresponding columns need to be compatible (implicitly convertible)
- A comparison between two NULLs yields true. Comparison between NULL and non-NULL yields false.
- Individual queries cannot have an ORDER BY clause
- You can add an ORDER BY clause at the end of the query so that it acts on the result of the set operator
- Columns names are determined by the first query.
What does UNION do?
Syntax for UNION
When do you use UNION ALL?
If the sets you’re unifying don’t have duplicate rows, should you use UNION or UNION ALL?
Combines results of queries vertically
SELECT country, region
FROM HR.Employees
UNION
SELECT country, region
FROM Sales.Customers;
Use UNION ALL when you want to keep duplicate rows (They are excluded if just use UNION)
If no duplicates, use UNION ALL so that SQL doesn’t have to check for duplicates and thus save computation cost
What does INTERSECT do?
Syntax for INTERSECT
Returns distinct rows that are in both queries
SELECT country, region, city
FROM HR.Employees
INTERSECT
SELECT country, region, city
FROM Sales.Customers;
What does EXCEPT do?
Syntax for EXCEPT
Returns rows that are in the first query but not in the second.
SELECT country, region, city
FROM HR.Employees
EXCEPT
SELECT country, region, city
FROM Sales.Customers;
What is the order of operations for the set operators?
INTERSECT precedes UNION and EXCEPT
UNION and EXCEPT are evaluated left to right based on their position in the expression.
Note: can use parenthesis to specify order
What is a CROSS JOIN?
CROSS JOIN syntax
It produces the Cartesian product of the two tables
SELECT D.n AS theday, S.n AS shiftno
FROM dbo.Nums AS D
CROSS JOIN dbo.Nums AS S
Consider this query:
SELECT D.n AS theday, S.n AS shiftno FROM dbo.Nums AS D CROSS JOIN dbo.Nums AS S WHERE D.n <= 7 AND S.n <= 3 ORDER BY theday, shiftno;
SQL Server performs optimization technique called predicate pushdown. What does this mean?
SQL Server knows that with a cross join followed by a filter it can evaluate the filters first.
What is an INNER JOIN?
INNER JOIN syntax
What’s equivalent to INNER JOIN?
Combines tables horizontally based on matching values in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
JOIN is equivalent to INNER JOIN
For inner joins, what’s the difference between ON and WHERE clauses?
What about outer joins?
There’s no difference. Instead of using a WHERE clause, you can simply put the expression on the ON clause
In outer joins, ON and WHERE play different roles. ON matches while WHERE filters.