• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/109

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

109 Cards in this Set

  • Front
  • Back
What is the order in which query clauses are processed?
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
What type of predicate logic does t-sql use?
Three valued predicate logic: TRUE, FALSE and UNKNOWN.
What are the four table operators taht SQL 2008 supports?
JOIN, APPLY, PIVOT, and UNPIVOT (the last 3 were introduced in SQL 2005)
What are the three types of JOINS?
CROSS, INNER and OUTER
What is a self-join?
A JOIN operation (CROSS, INNER or OUTER) against two instances of the same table.
Why are table aliases not optional in self-joins?
Without table aliases, all column names in the result of the join would be ambiguous.
How do yo start and end a block comment in SQL?
start: /*
end: */
What is a composite join?
A join based on a predicate that involves more than one attribute from each side, a primary-foreign key relationship where the relationship is composite.

FROM dbo.Table1 as T1
JOIN dbo.Table2 as T2
ON T1.col1 = T2.col1
AND T1.col2 = T2.col2
What is a non-equi join?
A join when the condition involves any operator besides equality, e.g. <, >.
What are the three logical phases of outer joins?
Outer joins apply the two logical phases that inner joins apply (Cartesian product and the ON filter) as well as a third phase called Adding Outer Rows.
What does RDBMS stand for?
Relational Database Management System.
What two branches of mathemtatics is the relational model built on?
Set theory and predicate logic
When would a table be considered a multiset or bag?
When it does not possess a key--you will not be able to uniquely identify rows.
What is a predicate?
A property or expression that either holds or doesn't hold, i.e. true or false.
What is a relation in mathematical terms?
A relation is a representation of a set in set theory; in the relational model, it is a table.
What is a proposition?
A proposition is an assertion or a statement that must be true or false, e.g. employee Itzik Ben-Gan was born on Freb. 12, 1971 and belongs to the IT dept.. If the proposition is true, it will appear as a row in the table.
What is a candidate key?
A candidate key is a key defined on one or more attributes preventing more than one occurrence of the same tuple (row) in a relation.
What are the requirements for 1NF?
Rows in the table must be unique and attributes should be atomic.
What are the requirements for 2NF?
1. data must meet the 1NF requirements.
2. For every candidate key, every nonkey has to be fully functionally dependent on part of the candidate key.
What are the requirements of 3NF?
1. Data must meet requirements for 2NF.
2. All nonkey attributes must be mutually independent.
Why is an OLTP environment not suitable for reporting purposes?
A normalized model usually involves many tables (one for each entity) with complex relationships. Even simple reports require joining many tables, resulting in complex and poorly performing queries.
What is a data mart?
A data warehouse that only serves part of an organization.
What is ETL?
Extract transform and load--the process that puls data from source systems (OLTP and others), manipulates it, and loads it into a data warehouse. SQL Server's tool for this is SSIS.
What is OLAP?
Online Analytical Processing.--in SQL Server, the product is SSAS.
What is the language used to manage and query SSAS cubes?
Multidimensional Expressions (MDX).
What is the language SSAS uses to manage and query data mining models?
Data Mining Extensions (DMX).
What is the DB_ID function?
The DB_ID function accepts a database name as input and returns its internal database ID. If a databse with the input name does not exist, the function returns a NULL.
IF DB_ID('testdb') IS NULL
CREATE DATABASE testdb;
What is the OBJECT_ID function?
The OBJECT_ID function accepts an object name and type as input. The type 'U' represents a user table. This function returns the internal object ID if an object with the given input name and type exists, and NULL otherwise.

IF OBECT_ID('dbo.Employees', 'U') IS NOT NULL
DROP TABLE dbo.Employees
What is a primary key constraint?
A primary key constraint enforces uniqueness of rows and also disallows NULLs in the constraint attribute.
What is a unique constraint?
A unique constraint enforces uniqueness of rows. Unlike primary keys, multiple unique constraints can be defined in the same table and unique contraints are not restricted to columns defined as NOT NULL.
What is the T-SQL code to create a primary key (PK_Employees) on the table dbo.Employees using the empid column?
ALTER TABLE dbo.Employees
ADD CONSTRAINT PK_EMPLOYEES
PRIMARY KEY(empid);
What is the TSQL code to create a unique constraint (UNQ_Employees) on the dbo.Employees table using the ssn column?
ALTER TABLE dbo.Employees
ADD CONSTRAINT UNQ_Employees_ssn
UNIQUE(ssn);
What is the TSQL code to create a FK (FK_Orders_Employees) constraint on the empid column in the dbo.Orders table pointing to the empid column in the dbo.Employees table?
ALTER TABLE dbo.Orders
ADD CONSTRAINT FK_Orders_Employees
FOREIGN KEY(empid)
REFERENCES dbo.Employees(empid);
What does the option ON DELETE CASCASE do?
When you delete a row from the referenced table, the RDBMS will delete the related rows from the referencing table.
What is a check constraint?
It allows you to define a predicate that a row must meet to enter the table or to be modified.

ALTER TABLE dbo.Employees
ADD CONSTRAINT CHK_Employees_salary
CHECK(salary > 0);
What is a default constraint?
It is an expression that is used as the default value when an explicit value is not specified for the attribute when you insert a row.

ALTER TABLE dbo.Orders
ADD CONSTRAINT DFT_Orders_ordersts
DEFAULT(CURRENT_TIMESTAMP) FOR orderts;
What does a query with an ORDER BY clause return?
A cursor--a nonrelational result with order guaranteed among rows.
What is the only phase processed after the SELECT phase?
The ORDERY BY phase.
If you use DISTINCT in the SELECT list, can you ORDER BY an expression not listed in the SELECT list?
No; a single result row might represent multiple source rows; therefore, it might not be clear which of the multiple possible values in the ORDER BY expression should be used.
How is the WITH TIES option used?
When specified with TOP, it returns all other rows from the table that have the same sort value as the last one found.
e.g.
SELECT TOP (5) WITH TIES orderid, orderdate
FROM Sales.Orders
ORDER BY orderdate DESC;
How do you use TOP with percentage?
SELECT TOP (1) PERCENT orderid, orderdate
FROM Sales.Orders
ORDER BY orderdate DESC;
Where can you use the OVER clause?
Only in the SELECT or ORDER BY phases.
What is the result of using empty parentheses with the OVERY clause?
It exposes all rows available after the FROM, WHERE, GROUP BY and HAVING phases are complete to the calculation.
If you want to restrict the rows that an OVER clause operates on, what can you specify?
You can use the PARTITION BY clause:
SELECT SUM(val) OVER(PARTITION BY custid) AS custototalvalue
What are the four ranking functions?
ROW_NUMBER
RANK
DENSE_RANK
NTILE
What does ROW_NUMBER do?
It assigns incrementing sequential integers to the rows in the result set of a query based on the logical order that is specified in the ORDERY BY subclause of the OVER clause.
SELECT ROW_NUMBER() OVER(ORDER BY val) AS rownum
What is the difference between RANK and DENSE_RANK?
RANK indicates how many rows have a lower ordering value while DENSE_RANK indicates how many distinct ordering values are lower.
SELECT orderid,
RANK() OVER(ORDER BY val) AS rank,
DENSE_RANK() OVER(ORDER BY val) AS dense_rank
What does the NTILE function do?
It allows you to associate the rows in the result with tiles--you specify how many--by assigning a tile number to each row.
SELECT NTILE(10) OVER(ORDER BY val) AS ntile
Why should you not specify both DISTINCT and ROW_NUMBER in the same SELECT statement?
The DISTINCT cluase would have no effect.
How do you use the IN predicate?
SELECT orderid, empid, orderdate
FROM Sales.Orders
WHERE orderid IN(10248, 10249, 10250);
How do you use the BETWEEN predicate?
SELECT orderid, empid, orderdate
FROM Sales.Orders
WHERE orderid BETWEEN 10300 AND 10310;
How do you use the LIKE predicate?
SELECT empid, firstname, lastname
FROM HR.Employees
WHERE lastname LIKE N'D%';
What does the expression 5/2 return?
the integer 2
What are the two forms of a CASE expression?
Simple and searched.
What is a simple CASE expression.
It compares one value, or scalar expression, with a list of possible values and returns a value back for the first match. If no match, it returns the value in the ELSE clause (if it exists) or a NULL if it does not.
What is a searched CASE expression?
It allows you to specify predicates or logical expressions in the WHEN clause rather than restricting to equality comparisons. e.g.
SELECT orderid, custid, val,
CASE
WHEN val < 1000.00 THEN 'Less then 1000'
WHEN val BETWEEN 1000.00 AND 3000.00 THEN 'Between 1000 and 3000'
WHEN val > 3000.00 THEN 'More than 3000'
ELSE 'Unknown'
END AS valuecategory
FROM Sales.OrderValues;
What is meant by SQL's all at once operations?
All expressions that appear in the same logical query processing phase are evaluated as if at the same point in time.
What are the two character data types that SQL Server supports?
regular (CHAR and VARCHAR) and Unicode (NCHAR and NVARCHAR)
What is the difference between regular and Unicode data types?
Regular data types use one byte of storage for each character while Unicode characters require two bytes per character.
How do you express a regular character literal?
Use single quotes:
'This is a regular character string literal'
How do you express a Unicode character literal?
You need to specify the character N (for National) as a prefix:
N'This is a Unicode character string literal'
Why are fixed length data types (e.g. CHAR(25) more suited for write-focused systems?
SQL server preserves the space, e.g. 25 characters, in the row regardless of the length of the stored character string, so no expansion of the row is required when the strings are expanded. This might result in less than optimal reads however.
How much storage space is required for VARCHAR or NVARCHAR data types?
SQL server uses as much storage space in the row as required to store the characters that appear n the character string plus two extra bytes for offset data.
How are read and update operations affected by VARCHAR or NVARCHAR as compared to CHAR AND NCHAR?
Because storage consumption is reduced when compared to fixed-length types, read operations are faster, but updates may result in row expansion which might cause data to be moved outside the current page (and would be less efficient).
What is the default threshold for variable-length data types with the MAX specifier (e.g. VARCHAR(MAX)? Where is the data stored?
8,000 bytes. It is stored inline in the row. A value size above threshold is stored external to the row as a large object (LOB).
What is collation?
A property of character data that encapsulates several aspects, including language support (relevant to regular types as Unicode supports all languages), sort order, case sensitivity, accent sensitivity, etc.
How do you get the set of supported collations and their descriptions?
You can the use table function: fn_helpcollations as follows:
SELECT name description
FROM sys.fn_helpcollations();
If BIN appeared in a collation name, what would it indicate?
Sorting and comparison of character data is based on binary representation of characters ('A' < 'B' < 'a' < 'b')
What does CI in the collation name indicate?
The data is case insensitive ('A' = 'a')
What does AS in a collation name indicate?
The data is accent sensitive.
What are the four levels that collation can be defined for?
instance, database, column, expression
(the lowest level is the effective one)
If you want to make a filter case sensitive when the column's collation is case insensitive, what can you use?
CAST, e.g.
SELECT empid, firstname, lastname
FROM HR.Employees
WHERE lastname COLLATE Latin1_General_CS_AS = N'davis';
How do you delimit irregular identifiers like table or column names that include a space or start with a digit?
Double quotes., e.g.
"Irregular Identifier"
SQL also supports brackets
[Irregular identifier]
How would you delimit a string that has a single quote character as part of the string, .e.g. abc'de?
Specify two single quotes
'abc' 'de'.
What is the result of concatenation with a NULL?
It results in a NULL.
Although not recommended, how could you change the default behavior of the way SQL treats concatenation with NULL?
SET CONCAT_NULL_YIELDS_NULL OFF;
SQL will treat a NULL as an empty string
What can you use to substitute a NULL with an empty string?
you can use the COALESCE function. This function accepts a list of input values and returns the first that is not NULL.
SELECT custid, country, region, city, country + N',' + COALESCE(region, N'') + N',' + city AS location
FROM Sales.Customers;
What does the SUBSTRING function do?
It extracts a substring from a string:
SUBSTRING( string, start, length )
SELECT SUBSTRING('abcde', 1, 3); returns 'abc'
What are the LEFT and RIGHT functions?
They are abbreviations of the SUBSTRING function, returning a requested number of characters from the left or right of the input string:
LEFT(string, n) where n is the number of charcterts to extract
e.g. SELECT RIGHT('abcde', 3); returns cde
What does the LEN function do?
Returns the number of characters in the input string:
LEN(string);
How would you get the number of bytes in a string?
Use the DATALENGTH function:
SELECT DATALENGTH(N'abcde'); returns 10.
How do LEN and DATALENGTH differ in their treatment of leading blanks?
LEN ignores then while DATALENGTH does not.
What does the CHARINDEX function do?
It provides the position of the first occurrence of a substring within a string:
CHARINDEX(substring, string[, start_pos])
If the substring is not found, it returns 0.
What does the PATINDEX function do?
It returns the position of the first occurrence of a pattern within a string:
PATINDEX(pattern, string)
e.g.
SELECT PATINDEX('%[0-9]%', 'abcd123efgh'); returns 5.
What does the REPLACE function do?
It replaces all occurrences of a substring with another:
REPLACE(string, substring1, substring2)
e.g.
SELECT REPLACE('1-a 2-b', '-', ':'); returns: '1:a 2:b'
How could you use the REPLACE function to count the number of occurrences of a character within a string?
Replace all occurences of the character with an empty string and calculate the original length minus the new:
LEN(lastname) - LEN(REPLACE(lastname, 'e', '')) AS numoccur
What does the REPLICATE function do?
It replicates a string a requested number of times:
REPLICATE(string, n), e.g.
SELECT REPLICATE('abc', 3) returns 'abcabcabc'
What does the STUFF function do?
It allows you to remove a substring from a string and insert a new substring instead:
STUFF(string, pos, delete_length, insertstring)
What doe the UPPER and LOWER functions do?
Return the string with all uppercase or lowercase, e.g.
SELECT UPPER('hello') returns 'HELLO'
What do the RTRIM and LTRIM functions do?
They return the input string with leading or trailing spaces removed, e.g.
SELECT RTRIM(LTRIM(' abc ')); returns 'abc'
What does a % sign represent when used with the LIKE predicate?
A string of any size, e.g.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'D%';
What does an underscore mean when used with the LIKE predicate?
It represents a single character.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'_e%';
What do square brackets mean when used with the LIKE predicate?
They represent a single character that must be one of the characters specified in the list.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'[ABC]%';
What do square brackets with a range represent when used with the LIKE predicate?
They represent a single character that must be in the range specified.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'[A-E]%';
What doe square brackets with a caret sign followed by a character list or range represent when used with the LIKE predicate?
They represent a single character no in the specified character list or range.
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'[^A-E]%';
How would you look for a character that is also used as a wildcard, e.g. ('%', '_', '[', ']')?
You can specify a character you know for sure doesn't appear in the data as an escape character in front of the escape character you are looking for and specify ESCAPE right after the pattern:
LIKE ‘%!_%' ESCAPE ‘!'
Prior to SQL 2008, what were the two temporal data types?
DATETIME and SMALLDATETIME
What are the fournew temporal data types that SQL 2008 introduced?
DATE
TIME
DATETIME2
DATETIMEOFFSET
Why is it best to use language-neutral formats like 'YYYYMMDD'?
They are interpreted the same way independent of language or date format settings.
How is the use of indexes impacted when you apply manipulation to the filtered column, such as:
WHERE YEAR(orderdate) = 2007;?
In most cases, you cannot use an index in an efficient manner.
How could you get the current system time?
SELECT CAST(SYSDATETIME() AS TIME) AS [current_time];
How could you get the current system date?
SELECT CAST(SYSDATETIME() AS DATE) AS [current_date];
What is the syntax for CAST?
CAST(value AS datatype)
What is the syntax for CONVERT?
CONVERT(datatype, value [, style number])
Why should you use CAST over CONVERT?
CAST is ANSI and CONVERT is not.
What does the SWITCHOFFSET function do?
It adjusts an input DATETIMEOFFSET value to a specified time zone.
SWITCHOFFSET(datetimeoffset_value, time_zone)
e.g. the following code adjusts the current system datetimeoffset value to time zone -05:00:
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00');
What does the TODATETIMEOFFSET function do?
It sets the time zone offset of an input date and time value
TODATETIMEOFFSET(date_and_time_value, time_zone)
What does the DATEADD function do?
It allows you to add a specified number of units of a specified date part.
DATEADD(part, n, dt_val)
Valid values for the part input include year, quarter, month,
dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, and nanosecond
What does the DATEDIFF function do?
It returns the difference between two date and time values in terms of a specified date part.
DATEDIFF(part, dt_val1, dt_val2)