Chapter 1. Manage data with Transact - SQL Flashcards

1
Q

What are the mathematics roots that originated the relational model?

A

Set theory and Predicate Logic.

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

How SQL attempts to represent a relation?

A

With a table.

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

What does have a relation in the relational model?

A

A relation has a heading and a body. A heading is a set of attributes, each attribute has a name and a type. A body is a set of tuples, and each tuple’s heading is the relation’s heading. SQL represents the set of attributes -the heading- as columns, and the set of tuples -the body- as rows.

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

What is a set?

A

A set should be considered as a whole. This means that we don’t interact with individual elements of the set, rather with the set as a whole.

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

What’s a predicate?

A

A predicate is an expression that when attributed to some object, makes a proposition either true or false.

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

How a result can be relational?

A

The order needs to be arbitrary, this mean there’s not an order specified. The result set shouldn’t has duplicates. All columns or expression needs to be named, and each name needs to be unique.

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

How a column can be aliased?

A

By using the keyword “AS” after the column or an expression. For example:

SELECT col1, col2 AS Columna, col3
FROM SampleTable;

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

What means three valued predicate logic?

A

It means a predicate can be either true, false or unknown. It’s unknown when at least one of the operand has a missing value (NULL in SQL).

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

What’s the difference between called row as record, and column as field?

A

Fields and records are physical. Fields are what you have in user interfaces in client applications, and records are what you have in files and cursors. Tables are logical, and they have logical rows and columns.

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

What’s NULL?

A

It’s a mark that represent missing values.

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

What’s the keyed-in-order of a query?

A

It’s the order in which the clauses of a query should be written:

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the logical query processing?

A

It’s how logically a query is processed:

  1. FROM
  2. ON
  3. OUTER
  4. WHERE
  5. GROUP BY
  6. CUBE | ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
    10 ORDER BY
  10. TOP
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s the difference between the WHERE clause and the HAVING clause?

A

The WHERE it’s evaluated before the rows are grouped, so it evaluates the predicate per row. The HAVING clause it’s evaluated after the rows are grouped, so it evaluates the predicate per group.

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

Why an alias created in the SELECT clause doesn’t wok in the FROM clause?

A

Because the FROM clause it’s evaluated before the SELECT clause, so the alias created in the SELECT clause isn’t yet created at the FROM clause.

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

What is the difference between T-SQL and SQL?

A

SQL is a standard, while T-SQL is a dialect of SQL.

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

What are the properties of the relational model?

A

(1) A relation should be considered as a whole - don’t work with one row at a time.
(2) A relation should not have duplicates.
(3) A relation should have no relevance to the order.
(4) All attributes must have names.
(5) All attribute names must be unique.

17
Q

Why are you not allowed to refer to a column alias defined by the SELECT
clause in the same SELECT clause?

A

Because all expressions that appear in the same logical query processing phase are evaluated conceptually at the same point in time.

18
Q

What does do the UNION operator?

A

It unify two result sets into one.

19
Q

What are the set operators availables in SQL Server?

A

UNION (UNION ALL, which is an extend of UNION operator), INTERSECT and EXCEPT.

20
Q

What are the requirements to use a set operator in SQL Server?

A

(1) The two queries needs to have the same number of columns. In case there’s a mismatch between columns number, then NULL needs to be used as placeholder.
(2) The data types of columns from query A, for example, needs to be the same (or at least compatible) with the data types of columns from query B.

21
Q

What are some constraint for UNION operator?

A

(1) Comparing NULL against NULL returns true (isn’t like WHERE clase which returns unknown and get discarted). But NULL against a value returns false. This mean all rows from one operand will be comparated to all rows from the other operand.
(2) An ORDER BY clause can be added, but it needs to be on the end of the last query, so individual ORDER BY aren’t allowed for each query.
(3) The columns names of result columns are determined by the first query.

22
Q

What’s the difference between UNION and UNION ALL?

A

UNION doesn’t return duplicates, whereas UNION ALL does.

23
Q

Do UNION and UNION ALL produce the same query plan?

A

No, they’ don’t. UNION has an implicity DISTINCT operator once unifies the result sets. Remember UNION doesn’t allow duplicates, whereas UNION ALL does.

24
Q

What does do the INTERSECT operator?

A

It returns the rows that are common to both sets. But in case a row appears twice in a set and just once in the other set, it doesn’t mean it’ll appear three times in the result set produced by the INTERSECT operator, just one. This is because if a row appears in both set, no matter how many times appear in both set, it’ll be returned.

25
Q

What is the syntax to use a set operator?

A

[ORDER BY ];

::= {UNION [ALL] | INTERSECT | EXCEPT}

26
Q

What does do the EXCEPT operator?

A

It returns distinct rows that appear in the first set but not in the second. This mean if a row appears at least once in the first result set, and doesn’t appear in the second result set, it’ll be returned only once in the result set produced by the EXCEPT operator.

27
Q

Does the order of the input queries affect the result set produced by the set operator?

A

With UNION and INTERSECT, the order of the input
queries doesn’t matter. However, with EXCEPT, there’s different meaning to:

EXCEPT
EXCEPT

28
Q

Does exist precedence between set operators?

A

Yes, it does. Finally, set operators have precedence: INTERSECT precedes UNION and EXCEPT, and UNION and EXCEPT are evaluated from left to right based on their position in the expression.

29
Q

What’s a cross join?

A

This join performs what’s known as a Cartesian Product. In other words, it performs a multiplication between two tables: yielding a row for each combination of rows from both sides. After all, it produces a m x n virtual table, where m is the row’s quantity from a table, and n is the row’s wquantity from the other table.

30
Q

What’s an inner join?

A

An inner join let you match two tables according to a predicate, usually comparing primary’s key from a table against a foreign key from another table which actually references the primary’s key of the former table.

31
Q

What’s an equijoin?

A

It’s simply an inner join where his predicate is based on equal operator, or has this form: lefttable.column = righttable.column.