Advanced SQL Topics and Query Design

0.0(0)
studied byStudied by 2 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/67

flashcard set

Earn XP

Description and Tags

Seventy question-and-answer flashcards covering joins, subqueries, derived tables, set operations, CASE logic, views, triggers, routines, temporal extensions, performance guidelines, and recent SQL enhancements.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

68 Terms

1
New cards

Why do we construct multiple-table queries in an RDBMS?

To leverage relationships between tables and combine related data into one result set.

2
New cards

What relationship links rows across tables by matching values in common columns?

A primary-key–foreign-key relationship.

3
New cards

Which clause of a SELECT statement normally specifies join conditions between tables?

The WHERE clause (or ON/USING inside the FROM clause).

4
New cards

What is the most frequently used relational operation in SQL?

The join operation.

5
New cards

List three common join types defined in the SQL standard.

INNER JOIN, OUTER JOIN (LEFT/RIGHT/FULL), and CROSS JOIN.

6
New cards

What rows does an INNER JOIN return?

Only rows that have matching values in both tables.

7
New cards

How does an OUTER JOIN differ from an INNER JOIN?

An OUTER JOIN also returns unmatched rows from one (LEFT/RIGHT) or both (FULL) tables, filling missing columns with NULLs.

8
New cards

Name the three outer-join variants supported by the ANSI syntax.

LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.

9
New cards

What is a Cartesian join and when does it occur unintentionally?

A join that returns every possible row combination of the participating tables; it happens when no join condition is supplied.

10
New cards

Which keyword explicitly requests a Cartesian product?

CROSS JOIN.

11
New cards

Define an equi-join.

A join that links tables by testing equality between common column values.

12
New cards

What advantage does the USING clause offer over ON in an equi-join?

It shortens syntax when the join columns share the same name in both tables.

13
New cards

Why is SQL called a set-oriented language with respect to joins?

Because it processes entire sets of rows at once rather than one row at a time.

14
New cards

What distinguishes a NATURAL JOIN from an equi-join?

A natural join removes duplicate common columns from the result.

15
New cards

When two tables share a column name, why must that column be qualified in a query?

To resolve ambiguity and tell the DBMS which table’s column is being referenced.

16
New cards

In an OUTER JOIN result, what appears in columns from the table that lacks a match?

NULL values.

17
New cards

Why must you test outer joins involving more than two tables carefully?

Different DBMSs may interpret complex outer-join syntax differently, producing unexpected results.

18
New cards

How many ON or WHERE conditions are needed when joining three tables A, B, and C?

Two conditions (A-B and B-C).

19
New cards

What is a self-join?

A query that joins a table to itself to compare rows within the same table.

20
New cards

Give one business situation that commonly requires a self-join.

Representing an employee-to-manager (supervises) relationship stored in one Employee table.

21
New cards

Why do self-joins use table aliases?

To distinguish the two logical copies of the same table in the query.

22
New cards

When would you use a subquery instead of a join?

When you need to filter results based on values derived from another SELECT but don’t need to display columns from that secondary table.

23
New cards

Which operator lets a subquery return multiple values to the outer query?

IN (or NOT IN).

24
New cards

What do EXISTS and NOT EXISTS test in SQL?

Whether the subquery returns any rows (non-empty set) or none (empty set).

25
New cards

Define a correlated subquery.

A subquery that references a column from the outer query and is executed once for each outer-query row.

26
New cards

What is a derived table?

A temporary result set produced by a subquery in the FROM clause that can be treated like a table in the outer query.

27
New cards

Why are derived tables useful when using aggregates such as AVG or MAX?

They let you compute the aggregate once and reference its value elsewhere in the outer query’s WHERE or SELECT clauses.

28
New cards

What requirement must all SELECTs in a UNION satisfy?

Each SELECT must return the same number of columns with compatible data types.

29
New cards

How does the CAST function assist when combining queries with UNION?

It converts column data types so that the result sets are compatible.

30
New cards

What do the INTERSECT and MINUS operators do?

INTERSECT returns rows common to both result sets; MINUS returns rows in the first set that are not in the second.

31
New cards

Which SQL keyword supplies IF-THEN-ELSE logic inside a query?

CASE.

32
New cards

Name the four forms of the CASE expression in SQL.

Expression-based CASE, predicate-based CASE, NULLIF, and COALESCE.

33
New cards

List two major advantages of using views.

They simplify complex queries for users and can hide sensitive columns to enhance security.

34
New cards

How does a dynamic view differ from a materialized view?

A dynamic view is recreated at query time and stores no data; a materialized view stores data physically and is refreshed periodically.

35
New cards

What does WITH CHECK OPTION guarantee when updating through a view?

That any insert or update performed through the view still satisfies the view’s defining WHERE condition.

36
New cards

Define a trigger in SQL.

A stored block of procedural code that the DBMS executes automatically in response to a specified event on a table or database.

37
New cards

What are the three parts of a typical trigger definition?

Event, condition (optional WHEN or FOR EACH ROW), and action.

38
New cards

How do routines (procedures/functions) differ from triggers?

Routines must be called explicitly; triggers fire automatically upon their defined event.

39
New cards

Give one administrative use of a DDL trigger.

Preventing accidental DROP or ALTER TABLE commands on production objects.

40
New cards

Why can cascaded triggers be dangerous?

They may cause unintended side effects or infinite loops if triggers fire other triggers repeatedly.

41
New cards

What does SQL/PSM stand for?

SQL Persistent Stored Modules.

42
New cards

In SQL terminology, what is a routine?

A stored procedure or function defined in the database.

43
New cards

How does a function differ from a procedure in SQL/PSM?

A function returns a single value and has only input parameters, while a procedure can have input, output, and input/output parameters.

44
New cards

List two advantages of SQL-invoked routines over embedding logic in each application.

Server-side routines are sharable by many programs and can be optimized centrally for performance.

45
New cards

Name two vendor-specific procedural SQL dialects.

Oracle PL/SQL and Microsoft Transact-SQL.

46
New cards

What business goal did the ProductLineSale PL/SQL procedure accomplish for Pine Valley Furniture?

It computed and stored sale prices for products by applying percentage discounts based on standard price.

47
New cards

What is the ‘impedance mismatch’ between SQL and many host languages?

SQL processes sets of rows at once, whereas many host languages process one record at a time.

48
New cards

What is the data dictionary in an RDBMS?

A collection of system tables that store metadata about database objects, users, and privileges.

49
New cards

Which Oracle data dictionary view shows all tables in the database?

DBA_TABLES.

50
New cards

Which SQL Server catalog view lists all database objects and their types?

sys.objects.

51
New cards

What major temporal feature did SQL:2011 add?

Support for application-time period tables and system-versioned tables to manage time-variant data.

52
New cards

What columns typically define an application-time period table?

A start date/time and end date/time that bound the validity period of each row’s attributes.

53
New cards

Name two new temporal predicates introduced in SQL:2011.

OVERLAPS and CONTAINS (others include PRECEDES, SUCCEEDS, etc.).

54
New cards

What does a system-versioned table automatically maintain?

Historical rows reflecting every insert, update, and delete, enabling auditing over time.

55
New cards

What is a bi-temporal table?

A table that combines application-time periods and system-versioning to track both business validity and transaction history.

56
New cards

Which two DML operations can the MERGE command perform in one statement?

INSERT and UPDATE (optionally DELETE in some DBMSs).

57
New cards

Which SQL:2016 feature improves time-series pattern detection?

Row Pattern Recognition using the MATCH_RECOGNIZE clause.

58
New cards

What do Polymorphic Table Functions (PTFs) allow you to do?

Write functions that return tables whose row types are determined at runtime, enabling flexible custom processing.

59
New cards

Why should SELECT * generally be avoided in production queries?

It retrieves unnecessary columns, increasing I/O and network traffic and reducing performance.

60
New cards

Why is keeping optimizer statistics up to date important?

Out-of-date statistics can cause the query optimizer to choose inefficient execution plans.

61
New cards

How does using compatible data types for columns and literals help query performance?

It avoids run-time data type conversions that slow processing.

62
New cards

What does the TOP operator (or FETCH FIRST) accomplish?

Returns only the first N rows or a percentage of rows from an ordered result set.

63
New cards

What is the first step in constructing a complex SQL query according to recommended practice?

Identify the desired result and list the required attributes (columns).

64
New cards

Name one efficiency strategy for writing faster SQL queries.

Minimize the number of subqueries or avoid correlated subqueries when possible.

65
New cards

How can temporary tables improve performance for a sequence of related queries?

They store intermediate results once so subsequent queries don’t need to recompute or rescan large base tables.

66
New cards

How do triggers differ from constraints in enforcing integrity?

Constraints are declarative and generally faster but less flexible; triggers allow complex logic but incur more overhead.

67
New cards

Which SQL:2008 addition lets you perform linear regressions and moving averages inside the database?

OLAP/analytical functions.

68
New cards

Why is JSON support in SQL:2016 significant?

It enables relational databases to store, retrieve, and manipulate semi-structured JSON data directly.