Chapter 3: Writing Simple SELECT Queries Flashcards
(125 cards)
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.