Chapter 2: Getting Started with the SELECT Statement Flashcards
What are the roles of the FROM clause?
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.
Is a good practice type the table with his schema?
Yes, it is. The way which we hit this practice is: [Schema].[Table].
What’s considered as best practice to alias a table in a query?
This way: [Schema].[Table] AS [Alias]
What is the convention for alias table?
The convention is use short name, and capital letter. For example, the table [HR].[Employee] should be aliased with the letter “E”.
Why this query falls: “SELECT E.empid, HR.Employees.firstname, lastname
FROM HR.Employees AS E;”?
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.
What are the roles of the SELECT clause?
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.
What’s the difference between DISTINCT clause and GROUP BY clause? Both launch the same results and execution plan!
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.
Why is considered as bad practice list all attributes of a table?
Because it hurts the perfomance of the system. It’s considered as good practice specify the columns which really needs.
Which way we can alias a column in a SELECT clause?
It’s considered as good practice alias an expression or a column in the SELECT clause like this way: [Column/Expression] AS [ALIAS].
What’s the difference between T - SQL SELECT clause and SQL SELECT clause?
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.
What’s the difference between a regular identifier and an irregular identifier?
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.
Mention different categories of data types
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 many bytes hold the INT type?
4 byte.
How many bytes hold the TINYINT type?
1 byte.
How many bytes hold the DATETIME type?
8 byte.
How many bytes hold the DATE type?
3 bytes.
How many bytes hold the FLOAT type?
8 bytes.
How many bytes hold the REAL type?
4 bytes.
What’s the difference between CHAR and VARCHAR?
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.
What’s the difference between CHAR and NCHAR?
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.
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”.
Functions with “TRY” as prefix returns NULL if the conversion doesn’t work correctly, while without “TRY” just fails.
What’s the difference between CAST, CONVERT and PARSE function?
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.
Explain the data type precedence.
/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.
How I can retrieve the current SQL Server Instance date and time?
With the GETDATE built-in function.