Write a Basic Query Flashcards

1
Q

A SQL query is a request for ___________ ?

A

Information.

Update 10/30/21, 3.3

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

Is SQL a declarative or imperative programming language?

A

Declarative (3.6)

We describe the result we want, but we can’t control how the result is generated. (3.6)

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

Name that Clause!

List the columns that should appear in your result grid.

A

The SELECT Clause

3.5

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

Name that Clause!

Specify the data sources used in your query, typically tables.

A

The FROM Clause

3.5

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

What is a query also know as?

A

A SELECT Statement.

3.5

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

SQL queries must be written with their clauses in what order?

A
  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY

(3.5)

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

SQL processes its clauses in what order?

A
  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

(3.5)

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

What does the asterisk mean in the following syntax?

SELECT *
FROM TableName

A

The asterisk (*) means “all columns,” and we have not limited the rows returned.

(3.7)

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

List and define the two types of comments?

A
  1. Line Comments - start with a double-dash ( – ), which will cause the rest of the line to become a comment.
  2. Block Comments - start with a forward-slash followed by an asterisk ( /* ), which will cause all text to be treated as a comment until it reaches an asterisk followed by a forward slash ( */ )

(3.7)

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

Do comments have to be on their own line?

A

Comments can start at any point during the query, on their own line or not.

(3.7)

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

How do comments provide a method of troubleshooting?

A

They can be used to temporarily disable sections of code. This can make it easier to selectively focus your attention on a certain section of code.

(3.8)

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

What does it mean to “comment out” a section of code?

A

Putting a section of code in a comment enables easily re-adding the code later by uncommenting the section of code: undoing the commenting syntax.

(3.8)

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

Which type of comment can be used to comment out code?

A

Both line comments and block comments can be used to comment out code.

(3.8)

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

How can you control the order the results are returned?

A

By using the ORDER BY clause.

3.11

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

What is the default order of the ORDER BY clause?

A

Ascending order.

3.11

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

What is the ascending order for values?

A

Small to high.

17
Q

What is the ascending order for text?

A

A to Z.

18
Q

How would you reverse sort the results of the ORDER BY clause?

A

By adding DESC after the column.

Example:
SELECT *
FROM PAT_ENC
ORDER BY CONTACT_DATE DESC

(3.11)

19
Q

How would you list the columns in the ORDER BY clause?

A

ORDER BY is followed by a comma-delimited list of columns that you want to sort in order or precedence.

(3.11)

20
Q

Why would you use the SELECT TOP statement?

A

To limit results to a number of rows or a percentage of total rows.

Example:
SELECT TOP 5 * FROM PATIENT = would return five rows from the PATIENT table
SELECT TOP 1 PERCENT * FROM PATIENT = would return one percent of the rows in the PATIENT table

(8.5)

21
Q

The SELECT and SELECT TOP statements are technically different statements. However, it can be useful to think of these as the same statement where the optional TOP keyword is evaluated after what clause?

A

The ORDER BY clause.

8.5

22
Q

Where are column aliases defined?

A

In the SELECT clause.

3.14

23
Q

Why can you use column aliases in the ORDER BY clause?

A

Because column aliases are defined in the SELECT clause and the SELECT clause is processed before the ORDER BY clause.

(3.14)

24
Q

What are two ways for defining aliases?

A
  1. You can use the AS keyword in a column alias to make it explicit, but it is not required.
  2. If the column alias has a space in it, it must be enclosed in double quotes (SQL Server also allows [square brackets] and ‘single quotes’)

(3.14)

25
Q

Does SQL ignore whitespace?

A

Yes, so adding spaces and carriage returns makes the code more readable while maintaining functionality.

(3.15)

26
Q

Do stylistic choices have any effect on the SQL query or the results?

A

No, stylistic choices make the code more readable while maintaining functionality.

(3.15)

27
Q

How is atomic code an example of stylistic choice?

A

Atomic code means that each individual piece of code is placed on its own line making it easier to see what is happening within different sections of code and to comment out single lines.

(3.15)

28
Q

Which clause in a SELECT statement is evaluated first?

A

FROM

3.18, Review Key

29
Q

Which clause in a SELECT statement is evaluated last?

A

ORDER BY

3.18, Review Key

30
Q

What is the syntax for a block (multi-line) comment?

A
/* Comment goes 
here */

(3.18, Review Key)

31
Q

What technique can be used to rename columns in a SQL query?

A

Aliasing

3.18, Review Key