3 Complex Queries Flashcards

1
Q

What SQL command is used to retrieve data from a database?

A

The SELECT command is used to retrieve data from a database.

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

How can you filter records in a SQL query?

A

By using the WHERE clause to specify conditions that the records must meet.

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

What does the percentage symbol % represent in a LIKE operator query?

A

The percentage symbol % represents any number of characters in a LIKE operator query.

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

What does the underscore symbol _ represent in a LIKE operator query?

A

The underscore symbol _ represents exactly one character in a LIKE operator query.

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

In SQL, what does the term ‘case-insensitive’ mean?

A

Case-insensitive means that the comparison does not distinguish between uppercase and lowercase letters.

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

What is the purpose of the NULL value in SQL?

A

NULL is used to represent missing or unknown data in a database.

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

What is a common use case for the DISTINCT clause in SQL?

A

To find unique values in a column

such as unique languages spoken in a country

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

How does the ORDER BY clause affect the result set of a query?

A

The ORDER BY clause sorts the result set by one or more specified columns, either in ascending or descending order.

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

What is the function of the AND operator in a WHERE clause?

A

The AND operator combines multiple conditions in a WHERE clause and requires all conditions to be true for a record to be included in the result set.

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

What is the function of the OR operator in a WHERE clause?

A

The OR operator combines multiple conditions in a WHERE clause and includes a record if at least one of the conditions is true.

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

What does the statement value NOT IN (value1, value2, ...) do?

A

It filters records where the specified value is not equal to any of the values in the list.

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

What is the difference between the BETWEEN operator and comparison operators?

A

The BETWEEN operator checks if a value falls within a range inclusively, while comparison operators check for less than, greater than, etc.

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

How can you use nested SELECT statements in SQL?

A

You can use nested SELECT statements (subqueries) to perform more complex queries where the result of one query is used in another.

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

What is the result of using the WHERE clause with a NULL value?

Example:
SELECT * FROM customers WHERE last_name IS NULL;

A
  1. A WHERE clause that checks for equality with NULL & will return no results.
  2. As NULL cannot be equal to anything, including itself.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the importance of primary keys in relation to data integrity in a database?

A

Primary keys ensure that each record in a table is unique and not null, maintaining data integrity within the database.

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

ABS(n)

A

Returns the absolute value of n

Example: SELECT ABS(-5); Result: 5

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

LOG(n)

A

Returns the natural logarithm of n

Example: SELECT LOG(10); Result: 2.302585092904046

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

POW(x, y)

A

Returns x to the power of y

Example: SELECT POW(2, 3); Result: 8

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

RAND()

A

Returns a random number between 0 (inclusive) and 1 (exclusive)

Example: SELECT RAND(); Result: 0.11831825703225868 (will vary)

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

ROUND(n, d)

A

Returns n rounded to d decimal places

Example: SELECT ROUND(16.25, 1); Result: 16.3

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

SQRT(n)

A

Returns the square root of n

Example: SELECT SQRT(25); Result: 5

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

CONCAT(s1, s2, …)

A

Concatenates string arguments

Example: SELECT CONCAT(‘Dis’, ‘en’, ‘gage’); Result: Disengage

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

LOWER(s)

A

Returns the lowercase version of s

Example: SELECT LOWER(‘MySQL’); Result: ‘mysql’

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

REPLACE(s, from, to)

A

Replaces occurrences of from with to in s

Example: SELECT REPLACE(‘This and that’, ‘and’, ‘or’); Result: ‘This or that’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
SUBSTRING(s, pos, len)
* Extracts a substring of *s* starting at *pos* with length *len* * For instance ```SELECT SUBSTRING(BestMove, 7, 6)``` Means that value in `BestMove` lets say "Mario Finale" starts at position 7 and is 6 characters long. Result: "Finale" ## Footnote Example: `SELECT SUBSTRING`('Boomerang', 1, 4); Result: 'Boom'
26
TRIM(s)
Removes leading and trailing spaces ## Footnote Example: `SELECT TRIM`(' test '); Result: 'test'
27
UPPER(s)
Converts *s* to uppercase ## Footnote Example: `SELECT UPPER`('mysql'); Result: MYSQL
28
What does the function CURDATE() return?
Returns the current date, time, or date and time in ``` 'YYYY-MM-DD', 'HH:MM:SS', or 'YYYY-MM-DD HH:MM:SS' format ``` ## Footnote SELECT CURDATE(); returns '2024-12-26'
29
What does the function CURTIME() return?
Returns the current date, time, or date and time in ``` 'YYYY-MM-DD', 'HH:MM:SS', or 'YYYY-MM-DD HH:MM:SS' format ``` ## Footnote SELECT CURTIME(); Returns '21:05:44'
30
What does the function NOW() return?
Returns the current date, time, or date and time in ``` 'YYYY-MM-DD', 'HH:MM:SS', or 'YYYY-MM-DD HH:MM:SS' format ``` ## Footnote SELECT NOW(); Returns '2019-01-25 21:05:44'
31
What is the purpose of the DATE(*expr*) function?
Extracts date from datetime expression ## Footnote SELECT DATE('2013-03-25 22:11:45'); Returns '2013-03-25'
32
What is the purpose of the TIME(expr) function?
Extracts time from datetime expression ## Footnote SELECT TIME('2013-03-25 22:11:45'); Returns '22:11:45'
33
What does the `DAY('d')` function return?
Returns the day from date `d` ## Footnote SELECT DAY('2016-10-25'); Returns 25
34
What does the MONTH(d) function return?
Returns the month from date d ## Footnote SELECT MONTH('2016-10-25'); Returns 10
35
What does the `YEAR('d')` function return?
Returns the year from date `d` ## Footnote SELECT YEAR('2016-10-25'); Returns 2016
36
What does the HOUR(t) function return?
Returns hour from time t ## Footnote SELECT HOUR('22:11:45'); Returns 22
37
What does the `MINUTE(t)` function return?
Returns minute from time t ## Footnote SELECT MINUTE ('22:11:45'); Returns 11
38
What does the `SECOND('t')` function return?
Returns second from time `t` ## Footnote SELECT SECOND('22:11:45'); Returns 45
39
What does the `DATEDIFF(expr1, expr2)` function return?
* Returns `expr1 - expr2` in number of days or time. * Values, given `expr1` and `expr2` are *date, time, or datetime* values ## Footnote SELECT DATEDIFF('2013-03-10', '2013-03-04'); Returns 6
40
What does the `TIMEDIFF('expr1', 'expr2')` function return?
* Returns `expr1 - expr2` in number of days or time. * Values, given `expr1` and `expr2` are date, time, or datetime values ## Footnote SELECT TIMEDIFF('10:00:00', '09:45:30'); Returns 00:14:30
41
# Date & Time functions The function _____ extracts the day from a date.
`DAY('d')`
42
# Date & Time functions The function _____ extracts the time from a datetime expression.
`TIME('t')`
43
What is the result format of the `CURDATE('d')` function?
`'YYYY-MM-DD'`
44
What is the result format of the `CURTIME('t')` function?
`'HH:MM:SS'`
45
What is the result format of the NOW() function?
`'YYYY-MM-DD', 'HH:MM:SS'`
46
What would be the result of `SELECT HOUR('22:11:45')`?
22
47
What would be the result? ``` SELECT DATEDIFF('2013-03-10', '2013-03-04') ```
6
48
What would be the result? ``` SELECT TIMEDIFF('10:00:00', '09:45:30') ```
00:14:30
49
# Refer to the table What would the SELECT statement produce? ``` SELECT TIME(Due) FROM Assignment WHERE ID = 2; ```
23:59:00 ## Footnote TIME() extracts the time from '2019-11-02 23:59:00'.
50
# Refer to the table What would the SELECT statement produce? ``` SELECT DAY(Due) FROM Assignment WHERE ID = 4; ```
14 ## Footnote DAY() extracts the day from '2019-11-14'
51
# Refer to the table What would the SELECT statement produce? ``` SELECT HOUR(Assigned) + MINUTE(Assigned) FROM Assignment WHERE ID = 2; ```
42 ## Footnote HOUR() extracts the hour from '12:30:00', and MINUTE() extracts the minute. 12 + 30 = 42.
52
# Refer to the table What would the SELECT statement produce? ``` SELECT DATEDIFF(Due, Assigned) FROM Assignment WHERE ID = 1; ```
1 ## Footnote DATEDIFF() calculates the number of days from '2019-11-01' to '2019-11-02', which is just 1 day.
53
# Refer to the `Problem` Table What are the results from each `SELECT` statement? ``` SELECT ABS(X - Y) FROM Problem; ```
``` 3 8 7 ``` ## Footnote `ABS(X - Y)` finds the absolute value of the `X` column values minus the `Y` column values.
54
# Refer to the `Problem` Table What are the results from each `SELECT` statement? ``` SELECT ROUND(X / Y, 0) FROM Problem; ```
``` 3 9 0 ``` * After dividing the numbers in the X column by the numbers in the Y column, the answer is rounded to 0 decimal places: 1. 5 / 2 = 2.5 rounded is 3, 2. 9 / 1 rounded is 9, and 3. 3 / 10 = 0.3 rounded is 0
55
# Refer to the `Problem` Table What are the results from each `SELECT` statement? ``` SELECT ROUND(SQRT(X), 1) FROM Problem; ```
``` 2.2 3.0 1.7 ``` * The `SQRT()` function finds the square root of the `X` column, then `ROUND()` rounds the result to one decimal place. * Ex: `SQRT(5) = 2.23606797749979`, and `ROUND(2.23606797749979, 1)` rounds to `2.2`.
56
# Refer to the `Problem` Table What are the results from each `SELECT` statement? ``` SELECT X, Y, POW(X, Y) FROM Problem; ```
* The first column is X, the next column is Y, and the last column is X to the power of Y. * Ex: `POW(5, 2) = 25`.
57
# Refer to the `Avatar` Table What are the results from each `SELECT` statement? ``` SELECT CONCAT('Super ', Name) FROM Avatar WHERE ID = 1; ```
``` Super Link ```
58
# Refer to the `Avatar` Table What are the results from each `SELECT` statement? ``` SELECT LOWER(BestMove) FROM Avatar WHERE ID = 3; ```
``` psystrike ```
59
# Refer to the `Avatar` Table What are the results from each `SELECT` statement? ``` SELECT SUBSTRING(BestMove, 7, 6) FROM Avatar WHERE ID = 4; ```
``` Finale ``` ## Footnote `SELECT SUBSTRING(BestMove, 7, 6)` means that value in `BestMove` : "Mario Finale" starts at position 7 and is 6 characters long.
60
# Refer to the `Avatar` Table What are the results from each `SELECT` statement? ``` SELECT REPLACE(Name, 'Kn', 'Fr') FROM Avatar WHERE ID = 2; ```
``` Meta Fright ``` ## Footnote `REPLACE()` replaces 'Kn' in 'Meta Knight' with 'Fr'.