Docsity

SQL Subqueries Notes for Data ana, Exams of Computer Science

Toggle sidebar
bg1
What are group functions in SQL? - Group functions, also called
aggregate functions, operate on sets of rows to provide a single result per
group.
List some common group functions in SQL. - AVG, COUNT, MAX,
MIN, STDDEV, SUM, and VARIANCE.
What does the AVG function do? - Calculates the average value of a
numeric data set.
What does the SUM function do? - Calculates the total sum of a numeric
data set.
What is the purpose of the COUNT function? - Counts the number of
rows in a table or the number of non-null values in a specific column.
What does COUNT(*) return? - The total number of rows in a table,
including rows with null values.
How does the COUNT(DISTINCT column_name) function work? - It
returns the number of unique, non-null values in the specified column.
What happens to null values in group functions by default? - Group
functions ignore null values automatically.
How can you include null values in calculations with group functions? -
By using the NVL function to convert null values to a specified number.
pf3
pf4
pf5

Partial preview of the text

Download SQL Subqueries Notes for Data ana and more Exams Computer Science in PDF only on Docsity!

What are group functions in SQL? - Group functions, also called aggregate functions, operate on sets of rows to provide a single result per group. List some common group functions in SQL. - AVG, COUNT, MAX, MIN, STDDEV, SUM, and VARIANCE. What does the AVG function do? - Calculates the average value of a numeric data set. What does the SUM function do? - Calculates the total sum of a numeric data set. What is the purpose of the COUNT function? - Counts the number of rows in a table or the number of non-null values in a specific column. What does COUNT(*) return? - The total number of rows in a table, including rows with null values. How does the COUNT(DISTINCT column_name) function work? - It returns the number of unique, non-null values in the specified column. What happens to null values in group functions by default? - Group functions ignore null values automatically. How can you include null values in calculations with group functions? - By using the NVL function to convert null values to a specified number.

What is the purpose of the GROUP BY clause? - It divides rows in a table into smaller groups for applying group functions to specific subsets of data. What must be included in the GROUP BY clause? - Any column in the SELECT list that is not part of a group function. What is the HAVING clause used for? - To filter or restrict groups after they have been created by the GROUP BY clause. What is the order of operations for processing data in SQL? - 1. Rows are grouped. 2. The group function is applied. 3. Groups matching the HAVING clause are displayed. What is a join in SQL? - A join is a SQL operation that combines data from two or more tables based on a related column. What are some types of SQL joins? - Cross joins, Natural joins, USING clause, Full outer joins, and arbitrary join conditions. What is the purpose of using table prefixes in SQL? - To qualify column names that exist in multiple tables and avoid ambiguity. What is a table alias in SQL? - A short nickname for a table that simplifies queries and improves performance. What does a NATURAL JOIN do? - Automatically joins tables based on all columns with the same name in both tables. What is the USING clause used for in SQL? - To specify which column should be used for an equijoin when multiple columns share names. What happens if matching columns in a NATURAL JOIN have different data types? - It returns an error.

What is a nonequijoin? - A nonequijoin is a join condition that uses something other than an equality operator, often involving a range. What operator is commonly used in nonequijoins? - The BETWEEN operator is commonly used in nonequijoins. What does a LEFT OUTER JOIN return? - A LEFT OUTER JOIN returns the results of an inner join plus unmatched rows from the left table. What is the difference between INNER JOIN and OUTER JOIN? - An INNER JOIN returns only matched rows, while an OUTER JOIN returns matched rows plus unmatched rows from one or both tables. What is a Cartesian product in SQL? - A Cartesian product occurs when every row in the first table is joined to every row in the second table, often due to a missing join condition. What is the syntax for a CROSS JOIN? - SELECT column1, column FROM table1 CROSS JOIN table2; What is a subquery? - A subquery is a query nested inside another query, used to find information needed for the main query. What must you do when using subqueries? - Enclose subqueries in parentheses and place them on the right side of the comparison condition. What is a single-row subquery? - A single-row subquery returns only one row of data back to the main query. What operators can be used with single-row subqueries? - Single-row comparison operators: =, >, >=, <, <=, <>.

What happens if a single-row subquery returns multiple rows? - It will cause an error (ORA-01427). What is a multiple-row subquery? - A multiple-row subquery returns more than one row back to the main query. What operators can be used with multiple-row subqueries? - Operators include IN, ANY, and ALL. What does the IN operator do in a multiple-row subquery? - It checks if a value is equal to any member in the list returned by the subquery. What does the ANY operator do in a multiple-row subquery? - It compares a value to each value returned by the subquery. What does the ALL operator do in a multiple-row subquery? - It compares a value to every value returned by the subquery. What is the syntax for a single-row subquery? - SELECT column FROM table WHERE column = (SELECT column FROM table WHERE condition); What is the syntax for a multiple-row subquery? - SELECT column FROM table WHERE column IN (SELECT column FROM table WHERE condition); When should you use subqueries? - Use subqueries when selecting rows based on conditions that depend on unknown data within the database. What is the purpose of the ORDER BY clause in a subquery? - An ORDER BY clause in a subquery is not needed unless performing Top- N analysis.