Skip to content
CS-504 (C) · Introduction to Database Management Systems/Quick Revision Short Notes

Introduction to Database Management Systems (CS-504 (C)) - Unit 5 Short Notes

How unit 5 is examined

This unit covers the SELECT statement, joins, special operators, subqueries, hierarchical and flashback queries, and PL/SQL-style blocks; the only asked topic is SELECT with GROUP BY and ORDER BY.

SELECT

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>SELECT retrieves rows and columns from one or more tables without changing the data.</mark>

SELECT [DISTINCT] col_list FROM table [WHERE cond]
[GROUP BY col_list [HAVING cond]] [ORDER BY col [ASC|DESC]];

Key points.

  1. GROUP BY groups rows with equal values so aggregates such as COUNT, SUM and AVG work per group, e.g. SELECT dept, AVG(sal) FROM emp GROUP BY dept;.
  2. HAVING filters groups after aggregation, while WHERE filters rows before it.
  3. ORDER BY sorts the result, ascending by default and descending with DESC, e.g. ORDER BY sal DESC.
  4. Execution order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.

Update and Delete syntax. UPDATE t SET col = val [WHERE cond]; and DELETE FROM t [WHERE cond];

Asked: [7 marks] (Jun 2020) Write syntax of SQL order by and group by clauses. Asked: [7 marks] (Dec 2020) Explain the following commands with syntax: Select, Update, Delete.

SQL queries

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. A query is a SQL statement that asks the database for data, and <mark>the basic structure of a query is SELECT, FROM and WHERE.</mark>

Key points.

  1. SELECT lists the columns, FROM names the tables, and WHERE gives the row condition.
  2. SELECT * returns all columns, and an alias renames a column, e.g. sal AS salary.
  3. A query can be nested inside another query.
  4. The result of a query is itself a table.

Asked: [7 marks] (Dec 2020) Short notes: Basic structure of SQL query.

Data extraction from single, multiple tables: equi-join, non equi-join, self-join, outer join

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A join combines rows of two or more tables on a related column.</mark>

Key points.

  1. Equi-join uses = between columns: SELECT e.name, d.dname FROM emp e, dept d WHERE e.dno = d.dno;.
  2. Non-equi-join uses another operator such as BETWEEN, e.g. e.sal BETWEEN g.lo AND g.hi.
  3. Self-join joins a table to itself using two aliases: FROM emp e, emp m WHERE e.mgr = m.empno.
  4. Outer join also keeps unmatched rows: LEFT, RIGHT or FULL OUTER JOIN, with NULL for missing values.

Special operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Special operators test patterns, sets and subquery results in a WHERE clause.</mark>

Key points.

  1. LIKE matches patterns, where % is any string and _ is one character: name LIKE 'A%'.
  2. IN tests membership of a list or subquery, and EXISTS is true if the subquery returns any row.
  3. ANY is true if the comparison holds for at least one value, e.g. sal > ANY (subquery).
  4. ALL is true only if it holds for every value, e.g. sal > ALL (subquery).

Hierarchical queries

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A hierarchical query retrieves rows of a tree-structured table using START WITH and CONNECT BY.</mark>

SELECT empno, ename, LEVEL FROM emp
START WITH mgr IS NULL CONNECT BY PRIOR empno = mgr;

Key points.

  1. START WITH gives the root rows, and CONNECT BY PRIOR gives the parent-child link.
  2. The pseudo-column LEVEL shows depth, with the root at 1.
  3. It is Oracle syntax, used for employee-manager and bill-of-material data.

Inline queries

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An inline query (inline view) is a subquery in the FROM clause that acts as a temporary table.</mark>

Key points.

  1. Example: SELECT * FROM (SELECT dno, AVG(sal) a FROM emp GROUP BY dno) WHERE a > 5000;.
  2. The inner query runs first and the outer query reads its result.
  3. It lets a WHERE filter use an aggregate computed in the inner query.
  4. Nothing is stored, so the view exists only for that statement.

Flashback queries

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A flashback query shows table data as it was at an earlier time, using undo data.</mark>

Key points.

  1. Syntax: SELECT * FROM emp AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '10' MINUTE);.
  2. AS OF SCN n gives the same view at a system change number.
  3. It helps recover wrongly deleted or updated rows.
  4. It works only while the undo data is still retained.

Introduction of ANSI SQL

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>ANSI SQL is the standard SQL language defined by ANSI and ISO so that it works across database products.</mark>

Key points.

  1. Versions include SQL-86, SQL-92, SQL:1999 and later.
  2. It defines DDL, DML and DCL statements.
  3. Vendors such as Oracle add extensions like PL/SQL on top of the standard.
  4. Standard code stays portable between systems.

Anonymous block

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An anonymous block is an unnamed PL/SQL block that runs once and is not stored in the database.</mark>

DECLARE v NUMBER := 5;
BEGIN
  DBMS_OUTPUT.PUT_LINE(v);
EXCEPTION WHEN OTHERS THEN NULL;
END;

Key points.

  1. DECLARE is optional, BEGIN and END are mandatory, and EXCEPTION is optional.
  2. It is compiled each time it runs.
  3. It cannot be called by name or take parameters.

Nested anonymous block

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A nested anonymous block is a block placed inside the executable or exception part of another block.</mark>

Key points.

  1. The inner block has its own DECLARE, BEGIN, EXCEPTION and END.
  2. Outer variables are visible inside, but inner variables are not visible outside.
  3. An exception in the inner block can be handled there so the outer block continues.
  4. An inner variable with the same name hides the outer one.

Branching and looping constructs

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Branching chooses a path by a condition, and looping repeats statements.</mark>

IF x > 0 THEN ... ELSIF x = 0 THEN ... ELSE ... END IF;
CASE grade WHEN 'A' THEN ... ELSE ... END CASE;
LOOP ... EXIT WHEN i > 5; END LOOP;
WHILE i <= 5 LOOP ... END LOOP;
FOR i IN 1..5 LOOP ... END LOOP;

Key points.

  1. Branching uses IF-THEN-ELSIF-ELSE and CASE.
  2. LOOP runs until EXIT WHEN is true, WHILE tests before each pass, and FOR runs a fixed range.
  3. Each IF ends with END IF and each loop with END LOOP.

Last-minute revision

  • SELECT clause order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
  • Execution order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
  • WHERE filters rows, HAVING filters groups.
  • ORDER BY is ascending by default; DESC reverses it.
  • Equi-join uses =, non-equi uses BETWEEN or other operators, self-join uses two aliases.
  • Outer join keeps unmatched rows with NULLs.
  • LIKE uses % and _; IN tests a set; EXISTS tests for rows; ANY and ALL compare with a set.
  • Hierarchical query: START WITH plus CONNECT BY PRIOR, with LEVEL.
  • Inline view is a subquery in FROM.
  • Flashback uses AS OF TIMESTAMP or AS OF SCN.
  • Anonymous block: DECLARE, BEGIN, EXCEPTION, END.

Memory hooks

  • "Where before Group, Having after": WHERE row filter, HAVING group filter.
  • "FWGHSO": From, Where, Group, Having, Select, Order is the execution order.
  • "Self-join = two nicknames" for one table with two aliases.
  • "Any = one, All = every."
  • "DBE" for Declare, Begin, Exception in a block.

Coverage checklist

  • SELECT: Jun 2020 order by and group by syntax; Dec 2020 Select, Update, Delete syntax.
  • SQL queries: Dec 2020 basic structure of SQL query.
  • Data extraction from single, multiple tables equi-join, non equi-join, self-join, outer join: covered.
  • Usage of like, any, all, exists, in Special operators: covered.
  • Hierarchical queries: covered.
  • inline queries: covered.
  • flashback queries: covered.
  • Introduction of ANSI SQL: covered.
  • anonymous block: covered.
  • nested anonymous block: covered.
  • branching and looping constructs in ANSI SQL: covered.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in