Chapter 2: Getting Started with the SELECT Statement Flashcards

1
Q

What are the roles of the FROM clause?

A

The FROM clauses has two roles:

(1) Indicate the table which will be queried.
(2) Apply set operators between tables, like JOIN, LEFT JOIN, and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is a good practice type the table with his schema?

A

Yes, it is. The way which we hit this practice is: [Schema].[Table].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s considered as best practice to alias a table in a query?

A

This way: [Schema].[Table] AS [Alias]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the convention for alias table?

A

The convention is use short name, and capital letter. For example, the table [HR].[Employee] should be aliased with the letter “E”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why this query falls: “SELECT E.empid, HR.Employees.firstname, lastname
FROM HR.Employees AS E;”?

A

Because when we alias a table, the original name of the table, in this case “HR.Employees”, doesn’t exist anymore in the query context. So it’ll fall when we try to reference the original name of the table. Instead of that, we can use the alias “E” or doens’t put a prefix in the column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the roles of the SELECT clause?

A

The SELECT clause has two roles:

(1) It evaluates expressions that define the attributes in the query’s result, assigning them with aliases if it is needed.
(2) Using a DISTINCT clause, you can eliminate duplicates in the results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the difference between DISTINCT clause and GROUP BY clause? Both launch the same results and execution plan!

A

DISTINCT clause eliminate duplicates in the result, and also groups the result. If we specify more than one column, it’ll group the results (like in the GROUP BY). While the GROUP BY clause, groups the results (also eliminate duplicates) but you can apply aggregate functions to the groups (with DISTINCT clause you cannot do that). So the real difference between both clauses is both groups the result (also eliminate duplicates), but with GROUP BY clause you can apply aggregate functions, while with DISTINCT clause you cannot do that, just show the distinct groups.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is considered as bad practice list all attributes of a table?

A

Because it hurts the perfomance of the system. It’s considered as good practice specify the columns which really needs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which way we can alias a column in a SELECT clause?

A

It’s considered as good practice alias an expression or a column in the SELECT clause like this way: [Column/Expression] AS [ALIAS].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the difference between T - SQL SELECT clause and SQL SELECT clause?

A

T-SQL allows to do a SELECT clause without a FROM clause. For example, SELECT 10 AS [Number]. This query doesn’t fall, it works.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the difference between a regular identifier and an irregular identifier?

A

An irregular identifier is a reserved T-SQL keyword, or start with a digit, or has embedded space, so it needs to be used with square brackets “[]”. While regular identifier the square brackets are just optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mention different categories of data types

A

Exact Numeric (INT, NUMERIC), character strings (CHAR, VARCHAR), Unicode character strings (NCHAR, NVARCHAR), approximate numeric (DECIMAL, FLOAT, REAL), binary strings (BINARY, VARBINARY), date and time (DATE, DATETIME, DATETIME2, SMALLDATETIME) and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many bytes hold the INT type?

A

4 byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many bytes hold the TINYINT type?

A

1 byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many bytes hold the DATETIME type?

A

8 byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many bytes hold the DATE type?

A

3 bytes.

17
Q

How many bytes hold the FLOAT type?

A

8 bytes.

18
Q

How many bytes hold the REAL type?

A

4 bytes.

19
Q

What’s the difference between CHAR and VARCHAR?

A

CHAR’s length is static, while VARCHAR’s length is dynamic. For example, CHAR(20). If the string has a length of 15 characters, his storage space will be for 20 characters (5 characters are not being used). Instead we use VARCHAR(20), if the string’s length is 15, the VARCHAR length will be automaticaly 15.

20
Q

What’s the difference between CHAR and NCHAR?

A

The former is for regular character (english language), while the latter is form Unicode characters (more than one language is supported). A regular character holds one byte per character, while Unicode characters holds two byte per character.

21
Q

What’s the difference between a function which has the “TRY” prefix and a function which doesn’t have the “TRY” as prefix? For example, “TRY_CAST” and “CAST”.

A

Functions with “TRY” as prefix returns NULL if the conversion doesn’t work correctly, while without “TRY” just fails.

22
Q

What’s the difference between CAST, CONVERT and PARSE function?

A

With CAST you just indicate the source value and the target type, with CONVERT you indicate the source value, the target type and a third argument which means the style/format of the target type; and with PARSE you use anyformat supported by .NET Framework.

23
Q

Explain the data type precedence.

A

/NOTE: Precedence means Priority in this context/

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type.

24
Q

How I can retrieve the current SQL Server Instance date and time?

A

With the GETDATE built-in function.

25
Q

What’s the difference between the function DATEPART and the set of functions YEAR, DAY, MONTH?

A

With the DATEPART function, you specify date part which you want (for example, year, month or day) and the date which will be extracted; whereas with the YEAR, DAY or MONTH function, you just specifiy the date and it’ll return the YEAR, DAY or MONTH, depends which one you use.

26
Q

What’s the difference between the function DATEPART and DATENAME?

A

DATEPART retrieves the value of the date, whereas the DATENAME function retrieves the name of the date. For example, DATEPART(month, ‘20170728’) retrieves ‘07’, while DATENAME(month, ‘20170728’) retrieves ‘July’ (depends the language’s sesion).

27
Q

Which are commonly date functions?

A

DATEADD and DATEDIFF. The former is for add a specific date to the date passed as argument (day, month or year), whereas DATEDIFF retrieves the difference between two dates depending which criteria is passed (day, month or year).

28
Q

What happens when you try to concatenate a string with a NULL using the ‘+’ operator?

A

It returns NULL.

29
Q

What happens when you try to concatenate a string with a NULL using the CONCAT function?

A

If it is NULL one operator, returns an empty string, so it’ll concatenate the non-empty string with the empty string, which originally was NULL.

30
Q

What are the functions to extract a substring from a string and know the position?

A

SUBSTRING(String sourceValue, int startPosition, int subStringLength): extract a string from another string, starting in the “startPosition” value.
LEFT(String sourceValue, int subStringLength): returns, from the beginning, a string from another string.
RIGHT(String sourceValue, int subStringLength): returns, from the end, a string from another string.

The functions above just extract a substring from a string.

CHARINDEX(String searchString, String sourceValue):
this function returns the position of the first ocurrence of “searchString” in the “sourceValue” string.

31
Q

What are the functions to know the length of a string?

A

LEN(String value): this function returns the quantity of characters in the given string (also removes the spaces),
DATALENGTH(String value): returns the number of bytes of the given string.

32
Q

What are the functions to alter a string?

A

REPLACE(String sourceValue, String stringPattern, String stringToReplace): find all ocurrences of the “stringPattern” in the “sourceValue” string and replace it with the “stringToReplace” value.
REPLICATE(String stringToRepliacate, int n): replicate a string “n” times.
STUFF(String sourceValue, int startPosition, int subStringToDeleteLength, String stringToReplaceAfterDelete): this function operates on an input string provided as the first argument; then, from
the character position indicated as the second argument, deletes the number of characters
indicated by the third argument. Then it inserts in that position the string specified as the
fourth argument.

33
Q

What are the functions to format a string?

A

UPPER: convert to uppercase all characters in the given string.
LOWER: convert to lowercase all characters in the given string.
LTRIM: delete spaces from the beginning.
RTRIM: delete spaces from the end.
FORMAT: READ THIS IN THE DOCUMENTATION.

34
Q

What are the forms of the CASE statement?

A
(1) Simple form: It is used for compare the input expression with one or multiple values. For example: SELECT CASE SecondLastName,
    WHEN Nationality = Dominican THEN 
          SecondLastName
   ELSE
       NULL
(2) Searched form: uses predicates with the input expression and return a results depends if the predicate came true. For example:
SELECT CASE SecondLastName,
    WHEN Salary > 5000 THEN 
          SecondLastName
   WHEN Salary <= 2000 THEN
          ...
   ELSE
      NULL