M6 SQL Flashcards

SQL joins, 1-row functions, and group functions (58 cards)

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

Which join returns all rows from the left table and matching rows from the right?

A

LEFT OUTER JOIN

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

Which join returns all rows from the right table and matching rows from the left?

A

RIGHT OUTER JOIN

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

Fill in the blank: A non-equality join uses comparison operators like <, >, BETWEEN instead of _____.

A

=

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

Which set operator combines results from two SELECTs and removes duplicates?

A

UNION

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

Which set operator combines all rows from two SELECTs including duplicates?

A

UNION ALL

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

Which set operator returns only the rows that appear in both SELECTs?

A

INTERSECT

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

Which clause is used in a traditional join method?

A

WHERE clause

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

How do you avoid Cartesian joins?

A

Include a proper join condition using ON or WHERE

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

What function extracts a portion of a string based on position?

A

SUBSTR

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

Which function returns the number of characters in a string?

A

LENGTH

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

Function that pads the right side of a string with a character?

A

RPAD

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

Function that removes a character from the left side of a string?

A

LTRIM

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

Function that removes a character from the right side of a string?

A

RTRIM

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

What function replaces single characters in a string with others?

A

TRANSLATE

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

What function joins two strings together?

A

CONCAT

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

What function converts all letters in a string to lowercase?

A

LOWER

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

What function capitalizes the first letter of each word in a string?

A

INITCAP

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

What function rounds a number to a specified decimal place?

A

ROUND

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

Function that truncates a number after a decimal place?

A

TRUNC

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

Which function returns the remainder of a division operation?

A

MOD

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

Function that returns the absolute value of a number?

A

ABS

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

What function raises one number to the power of another?

A

POWER

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

Function that adds a number of months to a date?

25
Function that returns the next occurrence of a specified weekday?
NEXT_DAY
26
Function that returns the last day of the month for a date?
LAST_DAY
27
What does CURRENT_DATE return?
The current date and time from the session's time zone
28
What does SYSDATE return?
The current date and time from the operating system
29
What does ROUND(date, 'MONTH') do?
Rounds the date to the nearest month
30
What does TRUNC(date, 'YEAR') do?
Truncates the date to the first day of the year
31
What does NVL(x, y) return if x is NULL?
y
32
What does TO_CHAR(date, 'YYYY-MM-DD') return?
A formatted string representing the date
33
What does TO_NUMBER('123') return?
The numeric value 123
34
What function compares a value to multiple values and returns a match?
DECODE
35
What expression provides complex IF-THEN-ELSE logic in SQL?
CASE expression
36
What function checks if a string matches a regular expression pattern?
REGEXP_LIKE
37
What function extracts part of a string matching a pattern?
REGEXP_SUBSTR
38
What regular expression pattern matches exactly three digits?
[0-9]{3}
39
What happens when functions are nested?
The inner function is evaluated first
40
Which function returns the average of a numeric column?
AVG
41
What function returns the maximum value in a column?
MAX
42
What function returns the minimum value in a column?
MIN
43
What group function determines how spread out data is?
VARIANCE
44
What keyword is used to include only unique values in an aggregate?
DISTINCT
45
What clause performs multiple GROUP BYs in one query?
GROUPING SETS
46
What clause creates hierarchical totals from GROUP BY columns?
ROLLUP
47
Can group functions be nested?
Yes, up to two levels deep
48
What is the correct order of SQL clauses: WHERE, HAVING, GROUP BY?
WHERE -> GROUP BY -> HAVING
49
Explain the primary difference between an inner join and an outer join.
An inner join returns rows only when there is a match in both tables involved in the join, effectively showing only intersecting data. In contrast, an outer join returns all rows from one specified table (the preserved table) and only the matching rows from the other table, including NULLs for non-matching rows from the second table.
50
When would you use a self-join, and what is a crucial syntax requirement for performing one?
A self-join is used when you need to establish a relationship between different rows within the same table, such as finding employees who referred other employees in a single employee table. A crucial syntax requirement is the use of table aliases, which makes Oracle 12c treat the single table as if it were two separate tables during the query execution.
51
Describe the purpose of the NVL function and provide a scenario where it would be essential.
The NVL function substitutes a specified value for a NULL value in a column. It is essential when performing arithmetic operations on columns that might contain NULLs, because any calculation involving a NULL value will result in NULL, leading to inaccurate results without NVL.
52
What is the DUAL table used for in Oracle SQL, and why is it beneficial?
The DUAL table is a special Oracle table with a single column and a single row, primarily used to test functions and expressions. It is beneficial because it allows you to execute SQL queries that do not require referencing actual user data tables, making it convenient for quick function testing or retrieving system values like SYSDATE.
53
Differentiate between the ROUND and TRUNC functions when applied to numeric data.
The ROUND function rounds a numeric value to a specified precision, adjusting the number up or down based on standard rounding rules. The TRUNC function, on the other hand, truncates a numeric value to a specified position by simply removing any digits after that position without rounding.
54
Explain the purpose of the INSTR function and how it differs from SUBSTR.
The INSTR function searches a string for a specified substring and returns the starting numerical position where the substring is found. SUBSTR extracts a portion of a string (a substring) based on a specified starting position and an optional length. INSTR locates a substring, while SUBSTR extracts a substring.
55
What is a Cartesian join, and how can it accidentally be created in a SQL query?
A Cartesian join, also known as a cross join, is created when the joining condition between two tables is accidentally omitted in the WHERE clause or explicitly stated using the CROSS JOIN keywords. It results in every possible row combination from the first table multiplied by every row from the second table (m * n rows).
56
When should the HAVING clause be used instead of the WHERE clause in a SELECT statement?
The HAVING clause should be used to restrict groups based on conditions involving aggregate functions, acting after the GROUP BY clause has organized the data. The WHERE clause, however, is used to filter individual rows before any grouping or aggregation takes place.
57
Describe the primary function of the TO_CHAR function and give an example of its use for a date.
The TO_CHAR function converts dates and numbers into a formatted character string, allowing for custom display formats. For example, SELECT TO_CHAR(SYSDATE, 'Month DD, YYYY') FROM dual; would display the current date in a format like "December 17, 2009."
58
What is the maximum depth for nesting group functions, and how does the evaluation order work when functions are nested?
Group functions can be nested to a maximum depth of two. When functions are nested, the inner function is always evaluated first, and its computed result is then passed as the input argument to the outer function for its execution.