Querying with Transact-SQL Flashcards
What is SQL?
Structured Query Language
Good to Remember
SQL is a declarative language and not a procedural language.
Describe what you want to get not how you want to get it
Key Points
KEY POINTS
- Transact-SQL is the language used to query data in Microsoft SQL Server and Azure SQL Database.
- Data is stored in tables, which may be related to one another through common key fields.
- Objects in a database are organized into schemas.
- The fully qualified naming syntax for an object is server_name.database_name.schema_name.object_name, but in most cases you can abbreviate this to schema_name.object_name.
The result of a SELECT statement is always a………………..
Virtual Table
What does the ‘AS’ statement do?
Assigns a new name or an alias name to a column
Waht is ‘NULL’
NULL is an unknown value and any column operation performed on NULL will return a NULL ( eg. 58 + NULL = NULL)
KEY POINTS
Use the SELECT statement to retrieve a rowset of data from tables and views in a database.
SELECT statements are written with the following clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. However, the query engine processes the clauses in this order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
In the SELECT clause, you can use * to return all columns, but generally you should specify explicit columns.
You can specify expressions in the SELECT clause to return the results of calculations.
You can use the AS keyword to specify aliases for columns in the rowset returned by the SELECT statement.
Can you Concatenate or add 2 different data types?
NO
Explicit Conversion of Data Types?
Data Types : KEY POINTS
Transact-SQL supports a wide range of data types, which can be broadly categorized as exact numeric,approximate numeric, character, date/time, binary, and other (which includes specialized data types for handling data such as XML and spatial data).
Some data types are compatible, and values can be implicitly converted between them. Conversion between other data types requires the use of explicit conversion functions.
Working with NULLs
CASE Expression Syntax
KEY POINTS : NULL Datatype
NULL is used to indicate an unknown or missing value. NULL is not equivalent to zero or an empty string.
Arithmetic or string concatenation operations involving one or more NULL operands return NULL. For example, 12 + NULL = NULL.
If you need to compare a value to NULL, use the IS operator instead of the = operator.
The ISNULL function returns a specified alternative value for NULL columns and variables.
The NULLIF function returns NULL when a column or variable contains a specified value.
The COALESCE function returns the first non-NULL value in a specified list of columns or variables).
You write a query that returns the Name and Price columns from a table named Product in the Production schema. In the resulting rowset, you want the Name column to be named ProductName.
Which of the following Transact-SQL statements should you use?
SELECT Name AS ProductName, Price FROM Production.Product;
You need to retrieve data from a column that is defined as char(1). If the value in the column is a digit between 0 and 9, the query should return it as an integer value. Otherwise, the query should return NULL.
Which two functions can you use to accomplish this?
- TRY_CAST
- TRY_CONVERT
You write a Transact-SQL query that returns the Cellphone column from the Sales.Customer table. Cellphoneis a varchar column that permits NULL values. For rows where the Cellphone value is NULL, your query should return the text ‘None’. Select the correct function to complete the following query:
SELECT FirstName, LastName, __________(Cellphone, ‘None’) AS Cellphone
FROM Sales.Customer;
The ISNULL function substitutes the specified value for NULL.
ISNULL (Transact-SQL) Function
Examples:
SELECT Description, DiscountPct, MinQty, ISNULL(MaxQty, 0.00) AS ‘Max Quantity’ FROM Sales.SpecialOffer;
Testing for NULL in a WHERE clause
SELECT Name, Weight FROM Production.Product WHERE Weight IS NULL;
Removing Duplicates
In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
Example
SELECT DISTINCT City FROM Customers;
SELECT COUNT (DISTINCT Country)
FROM Supplier;
SELECT DISTINCT Color,Size FROM Product;
How do you Sort Results?
ORDER BY
Example of Using ORDER BY and TOP expressions
SELECT TOP 10 Id, ProductName, UnitPrice, Package
FROM Product
ORDER BY UnitPrice DESC;
OFFSET FETCH Clause
The OFFSET-FETCH clause provides you with an option to fetch only a window or page of results from the result set. OFFSET-FETCH can be used only with the ORDER BY clause.
Limitations in Using OFFSET-FETCH
ORDER BY is mandatory to use OFFSET and FETCH clause.
OFFSET clause is mandatory with FETCH. You can never use, ORDER BY … FETCH.
TOP cannot be combined with OFFSET and FETCH in the same query expression.
The OFFSET/FETCH rowcount expression can be any arithmetic, constant, or parameter expression that will return an integer value. The rowcount expression does not support scalar sub-queries.
Example 1 Skip first 10 rows from the sorted result set and return the remaining rows.
SELECT First Name + ‘ ‘ + Last Name FROM Employees ORDER BY First Name OFFSET 10 ROWS;
KEY POINTS : ORDER BY
By default, the SELECT statement returns all rows. If mulitple rows contain the same values for every column, they are duplicated in the results. Using the DISTINCT keyword eliminates duplicates, ensuring that only onerow for each distinct combination of column values is returned.
The order of rows in the result of a SELECT statement is not guaranteed unless you explicitly specify one or more columns in an ORDER BY clause. You can specify sort direction as ASC (the default) or DESC.
You can combine the ORDER BY clause with the TOP keyword to retrict the results so that they include only the top n rows (where n is the number or percentage of rows you want to return).
You can implement a query to retrieve a specified “page” of results by using the OFFSET and FETCH keywords with the ORDER BY clause.
Filtering and Using Predicates
KEY POINTS
Use the WHERE clause to filter the results returned by a SELECT query based on a search condition.
A search condition is composed of one or more predicates.
Predicates include conditional operators (such as =, >, and <), IN, LIKE, and NOT.
You can use AND and OR to combine predicates based on Boolean logic.