Chapter 3: Writing Simple SELECT Queries Flashcards
Q: What is the purpose of the SELECT statement in T-SQL?
A: The SELECT statement is used to retrieve data from SQL Server.
Q: What is the minimum requirement for a T-SQL SELECT statement?
A: The word SELECT followed by at least one item in a SELECT list.
Q: What is a literal value in the context of a SELECT statement?
A: A literal value is a specific value defined explicitly in the SELECT statement, not retrieved from the database.
Q: How are string literal values designated in T-SQL?
A: String literal values are enclosed in single quotes (tick marks).
Q: What is the recommended practice regarding semicolons in T-SQL?
A: It is recommended to end all T-SQL statements with a semicolon, even though it is not currently required.
Q: How can you run only a portion of the code in a query window in Azure Data Studio (ADS) or SQL Server Management Studio (SSMS)?
A: Highlight the desired statements and press F5 or click Run/Execute.
Q: What does the Messages tab display when running a query?
A: The Messages tab shows the number of rows affected, error messages, or other informational messages.
Q: What clause specifies the table name in a SELECT statement?
A: The FROM clause specifies the table name.
Q: Provide the syntax for a SELECT statement with a FROM clause.
A: SELECT <column1>, <column2> FROM <schema>.<table>;</schema></column2></column1>
Q: Why is it good practice to include the schema name when specifying a table in a SELECT statement?
A: To eliminate confusion, as different schemas can contain tables with the same name but different structures or data.
Q: How can you retrieve all columns from a table in a SELECT statement?
A: Use the asterisk (*) symbol in the SELECT list, e.g., SELECT * FROM <table>;.
Q: Why should the asterisk (*) symbol be avoided in production applications?
A: It may negatively impact performance, cause application errors if table columns change, and could expose unnecessary or sensitive data.
Q: What is the best practice for writing SELECT statements in production?
A: Specify exactly the columns needed and limit the rows to only those required.
Q: What does the GO command do in T-SQL?
A: It separates code into distinct batches but does not perform any SQL operation itself.
Q: How can you simplify creating a SELECT list when working with a large table?
A: Right-click on the table and select the top 1000 rows, then modify the query by removing the TOP (1000) restriction and unwanted columns.
Q: Why are square brackets ([]) used around table or column names in SQL Server?
A: Square brackets allow you to use names that do not follow SQL Server’s naming rules or are reserved words.
Q: What is the purpose of the keyword AS in a SELECT statement?
A: The AS keyword specifies an alias for a column, allowing you to rename columns or give headers to literals in the result set.
Q: What happens if you omit a comma between two column names in a SELECT list?
A: The second column name will be treated as an alias for the first column, which may lead to unintended results.
Q: What should you do if an alias contains a space or is a reserved word?
A: Enclose the alias in square brackets ([]), single quotes (‘ ‘), or double quotes (“ “).
Q: Can you mix literals and column names in the same SELECT statement?
A: Yes, you can mix literal values and column names in one SELECT statement.
Q: How does IntelliSense assist in writing SQL queries?
A: IntelliSense provides suggestions and a list of columns or objects when typing, helping you write code faster and more accurately.
Q: How can you see a list of columns from a table using IntelliSense?
A: After aliasing a table in the SELECT statement (e.g., FROM HumanResources.Employee AS EMP), type the alias followed by a period (e.g., EMP.) to see the list of columns.
Q: What is the purpose of replacing * with an alias and a period in a SELECT statement?
A: It allows you to filter and choose specific columns interactively using IntelliSense.
Q: What is a common mistake to avoid when creating a SELECT list with aliases?
A: Forgetting to include a comma between two column names, which can unintentionally turn the second column name into an alias.
Q: Why is it important to use aliases in a SELECT statement?
A: Aliases provide clearer and more meaningful column headers in the query results.
Q: What does the TOP (1000) clause do in a SELECT statement?
A: It limits the query to return only the first 1000 rows of the result set.
Q: What is the recommended way to use IntelliSense to select a specific column from a table?
A: Begin typing the alias and column prefix, and press Tab to autocomplete the desired column name.
Q: Does SQL Server require T-SQL statements to be written on a single line?
A: No, SQL Server does not care if a statement is written on one line or formatted over several lines.
Q: Why is it recommended to format T-SQL code over multiple lines?
A: Formatting over multiple lines with major clauses on new lines makes the code easier to read.
Q: Should T-SQL keywords be written in uppercase?
A: While not required, using uppercase for keywords is recommended for better readability and professionalism.
Q: Are column and table names case-sensitive in SQL Server?
A: It depends on the database collation. Most databases are case-insensitive by default, but case-sensitive collation requires exact case matching.
Q: Why should semicolons be used in T-SQL statements?
A: While not always required, semicolons are a best practice and may become mandatory in future SQL Server versions.
Q: What are the purposes of using aliases in T-SQL?
A:
To provide a name for an expression.
To return user-friendly column names.
To reduce typing by shortening table names used multiple times in a query.
Q: When should single quotes be used in T-SQL?
A: Single quotes are typically used to enclose literal dates and strings, such as ‘Smith’ or ‘2020-01-01’.
Q: What is the purpose of square brackets or double quotes in T-SQL?
A: They are used to enclose names with spaces or reserved keywords, e.g., [SELECT] or “Employee ID”.
Q: Which type of quotation is more commonly used in T-SQL: square brackets or double quotes?
A: Square brackets are more commonly used than double quotes in T-SQL.
Q: Can single quotes be used for column aliases in T-SQL?
A: Yes, but it is recommended to reserve single quotes for literal values.
Q: What is an example of using single quotes, double quotes, and square brackets in one query?
A:
SELECT BusinessEntityID AS “Employee ID”, ‘2020-04-13’ AS “Current Date” FROM [HumanResources].[Employee];
Q: What is the best way to learn T-SQL according to the text?
A: Practice by writing and figuring out the code yourself, rather than only reading examples.
Q: Why is filtering data important in T-SQL?
A: Filtering data allows queries to return only a subset of rows from a table, which is often needed in applications to display relevant information.
Q: What clause is used to filter rows in a T-SQL SELECT statement?
A: The WHERE clause.
Q: What is the syntax for using a WHERE clause in a SELECT statement?
A:
SELECT <column1>, <column2>
FROM <schema>.<table>
WHERE <column> = <value>;</value></column></schema></column2></column1>
Q: What types of values are enclosed in single quotes in a WHERE clause?
A: Literal strings and dates, e.g., ‘John’ or ‘2020-01-01’.
Q: What are some common comparison operators used in the WHERE clause?
A:
= (equals)
!= or <> (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
Q: What do the != and <> operators mean in a WHERE clause?
A: Both operators mean “not equal to” and are interchangeable.
Q: How does the WHERE clause process rows in a query?
A: It evaluates expressions (predicates) to TRUE, FALSE, or UNKNOWN, returning rows where the expression is TRUE.
Q: What does the query WHERE FirstName > ‘M’ return?
A: It returns rows where the first name is alphabetically greater than ‘M’, including names like ‘Ma’ but not ‘M’.
Q: What does the query WHERE FirstName !> ‘M’ return?
A: It returns rows where the first name is not greater than ‘M’, including names that are ‘M’ or earlier in the alphabet.
Q: How do you write a one-line comment in T-SQL?
A: Use two dashes (–) before the comment text.
Q: How do you write multi-line comments in T-SQL?
A: Enclose the comment with /* at the beginning and */ at the end.