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.