SQL Fundamentals, Query Execution Order, and Relational Schema Architecture

Fundamental SQL Clauses and Order of Execution

  • Overview of Six Core SQL Clauses:

    • SELECT\text{SELECT}: Specifies the target columns or calculated fields to retrieve and display in the result set.
    • FROM\text{FROM}: Specifies the primary table or table sources from which data will be retrieved.
    • WHERE\text{WHERE}: Filters individual records prior to aggregation based on specified search conditions.
    • GROUP BY\text{GROUP BY}: Groups individual rows sharing common values across specified columns into summary rows or subgroups; enables evaluation of aggregate metrics across a group as a whole (such as calculating a minimum value within a subgroup, a maximum value of a group, or an average across multiple elements).
    • HAVING\text{HAVING}: Filters groups created by the GROUP BY\text{GROUP BY} clause based on group-level conditions; any query containing a HAVING\text{HAVING} clause must contain a preceding GROUP BY\text{GROUP BY} clause, though a GROUP BY\text{GROUP BY} clause does not strictly require a HAVING\text{HAVING} condition.
    • ORDER BY\text{ORDER BY}: Controls the final visual display order of output records (e.g., sorting in alphabetical or reverse-alphabetical order).
  • Written Clause Sequence (Fixed Syntax Order):

    • Step 1: SELECT\text{SELECT} (Always written first in query editors and SQL platforms).
    • Step 2: FROM\text{FROM} (Always written immediately after SELECT\text{SELECT}).
    • Step 3: WHERE\text{WHERE} (Written third when row filtering conditions exist).
    • Step 4: GROUP BY\text{GROUP BY} (Written fourth when aggregation grouping is required).
    • Step 5: HAVING\text{HAVING} (Written fifth when conditional filtering on aggregated groups is needed).
    • Step 6: ORDER BY\text{ORDER BY} (Written sixth to specify output presentation order).
  • Clause Usage Frequency:

    • SELECT\text{SELECT}, FROM\text{FROM}, and WHERE\text{WHERE} represent the three most frequently utilized clauses in database operations.
    • GROUP BY\text{GROUP BY}, HAVING\text{HAVING}, and ORDER BY\text{ORDER BY} are optional clauses invoked based on specific user requirements or task specifications.
  • Database Engine Execution Order (Internal Processing Sequence):

    • Phase 1 — FROM\text{FROM}: The database engine begins execution here to establish directional information and determine which physical table shell to access.
    • Phase 2 — WHERE\text{WHERE}: The engine applies row-level filters across raw database tables (which may contain hundreds of thousands or millions of rows) to trim down the dataset.
    • Phase 3 — GROUP BY\text{GROUP BY}: The filtered records are partitioned into distinct smaller subgroups.
    • Phase 4 — HAVING\text{HAVING}: Group-level conditions are evaluated against the partitioned subgroups.
    • Phase 5 — SELECT\text{SELECT}: The engine projects and extracts only the specified columns or expressions to display on screen.
    • Phase 6 — ORDER BY\text{ORDER BY}: The final extracted output is sorted according to the requested display format.
  • Conceptual Query Design Workflow:

    • First: Determine which target table to access (FROM\text{FROM}).
    • Second: Specify filtering criteria to apply to raw rows (WHERE\text{WHERE}).
    • Third: Determine if rows need partitioning into smaller subcategories (GROUP BY\text{GROUP BY}).
    • Fourth: Apply required conditional constraints to the formed groups (HAVING\text{HAVING}).
    • Fifth: Select specific columns to project on screen (SELECT\text{SELECT}).
    • Sixth: Define presentation sorting criteria (ORDER BY\text{ORDER BY}).

Logical Operators in SQL Filtering: AND and OR

  • Fundamental Mechanics of Logical Operators:

    • Logical operators serve as operational evaluation tools within conditional filtering statements, functioning analogously to arithmetic operators (++, −-, ×\times, ÷\div).
    • They evaluate Boolean expressions to return either a definitive TRUE\text{TRUE} or FALSE\text{FALSE} evaluation for dataset record filtering.
  • The AND\text{AND} Operator:

    • Rule of Evaluation: Requires every single condition combined under the operator to evaluate to TRUE\text{TRUE} for the final overall statement result to evaluate to TRUE\text{TRUE}.
    • Strictness: If any individual condition evaluates to FALSE\text{FALSE} (whether one condition, multiple conditions, or all conditions), the overall evaluation immediately becomes FALSE\text{FALSE}.
    • Scalability: Applies strictly regardless of the number of conditions combined (N=2,3,4,5,…N = 2, 3, 4, 5, \dots); all NN components must be strictly TRUE\text{TRUE}.
    • Truth Table Analysis for Two Conditions (C1 AND C2C_1 \text{ AND } C_2):
    • Condition 1 = TRUE\text{TRUE}, Condition 2 = TRUE\text{TRUE} →\rightarrow Final Result = TRUE\text{TRUE}
    • Condition 1 = TRUE\text{TRUE}, Condition 2 = FALSE\text{FALSE} →\rightarrow Final Result = FALSE\text{FALSE}
    • Condition 1 = FALSE\text{FALSE}, Condition 2 = TRUE\text{TRUE} →\rightarrow Final Result = FALSE\text{FALSE}
    • Condition 1 = FALSE\text{FALSE}, Condition 2 = FALSE\text{FALSE} →\rightarrow Final Result = FALSE\text{FALSE}
    • Illustrative Scenario: Playing a video game contingent on two conditions: C1C_1 = homework finished, C2C_2 = room clean.
    • Homework finished (Yes\text{Yes}) AND Room clean (Yes\text{Yes}) →\rightarrow Allowed to play (Yes\text{Yes}).
    • Homework finished (Yes\text{Yes}) AND Room clean (No\text{No}) →\rightarrow Allowed to play (No\text{No}).
    • Homework finished (No\text{No}) AND Room clean (Yes\text{Yes}) →\rightarrow Allowed to play (No\text{No}).
    • Homework finished (No\text{No}) AND Room clean (No\text{No}) →\rightarrow Allowed to play (No\text{No}).
  • The OR\text{OR} Operator:

    • Rule of Evaluation: Requires at least one condition among the evaluated criteria to evaluate to TRUE\text{TRUE} for the overall statement result to evaluate to TRUE\text{TRUE}.
    • Flexibility: Yields a FALSE\text{FALSE} output only when every single component condition evaluates to FALSE\text{FALSE}.
    • Truth Table Analysis for Two Conditions (C1 OR C2C_1 \text{ OR } C_2):
    • Condition 1 = TRUE\text{TRUE}, Condition 2 = FALSE\text{FALSE} →\rightarrow Final Result = TRUE\text{TRUE}
    • Condition 1 = FALSE\text{FALSE}, Condition 2 = TRUE\text{TRUE} →\rightarrow Final Result = TRUE\text{TRUE}
    • Condition 1 = TRUE\text{TRUE}, Condition 2 = TRUE\text{TRUE} →\rightarrow Final Result = TRUE\text{TRUE}
    • Condition 1 = FALSE\text{FALSE}, Condition 2 = FALSE\text{FALSE} →\rightarrow Final Result = FALSE\text{FALSE}
    • Mathematical Outcome Distribution (Two-Variable System):
    • AND\text{AND} produces a TRUE\text{TRUE} evaluation in exactly 14\frac{1}{4} (25%25\%) of condition combinations.
    • OR\text{OR} produces a TRUE\text{TRUE} evaluation in 34\frac{3}{4} (75%75\%) of condition combinations.
    • Illustrative Scenario: Receiving a free pizza contingent on two conditions: C1C_1 = possess a coupon, C2C_2 = birthday today.
    • Possess coupon (Yes\text{Yes}) OR Birthday today (No\text{No}) →\rightarrow Receive free pizza (Yes\text{Yes}).
    • Possess coupon (No\text{No}) OR Birthday today (Yes\text{Yes}) →\rightarrow Receive free pizza (Yes\text{Yes}).
    • Possess coupon (Yes\text{Yes}) OR Birthday today (Yes\text{Yes}) →\rightarrow Receive free pizza (Yes\text{Yes}).
    • Possess coupon (No\text{No}) OR Birthday today (No\text{No}) →\rightarrow Receive free pizza (No\text{No}).

Database Schema Architecture: Shoe Store Relational Model

  • Relational Database Structure Overview:

    • Graphical Schema Layout: Table names are situated at the top of entity boxes; white background fields denote table attributes/columns.
    • Schema Connectivity: Entities are linked across relational networks via primary key (PK) and foreign key (FK) constraints.
  • Product and E-Commerce Transaction Subsystem (5 Tables):

    • Customer Table (Customer):
    • customer ID (Primary Key)
    • customer's first name
    • last name
    • street address
    • city
    • state
    • postcode
    • country
    • phone
    • Sale Table (Sale):
    • sale ID (Primary Key)
    • sale date
    • customer ID (Foreign Key referencing Customer table)
    • tax
    • shipping
    • Sale Item Table (Sale Item):
    • sale ID (Foreign Key referencing Sale table)
    • product ID (Foreign Key referencing Product table)
    • item size
    • quantity
    • sale price
    • Product Table (Product):
    • product ID (Primary Key)
    • product name
    • manufacturer ID (Foreign Key referencing Manufacturer table)
    • composition
    • list price
    • gender
    • category
    • color
    • description
    • Manufacturer Table (Manufacturer):
    • manufacturer ID (Primary Key)
    • name of the manufacturer
    • address
    • city
    • state
    • postcode
    • phone
    • fax
    • contact
    • URL
  • Internal Human Resources Subsystem (3 Tables):

    • Employee Table (Employee):
    • employee ID (Primary Key)
    • first name
    • last name
    • address
    • city
    • state
    • zip
    • phone
    • manager ID
    • SSN (Social Security Number)
    • Salary Table (Salary): Linked internally to the employee model via Social Security Number/employee identification.
    • Wages Table (Wages): Linked internally to the employee model via Social Security Number/employee identification.

Core SQL Syntax, Queries, and Interface Rules

  • Minimum Working SQL Statement:

    • A minimum functional SQL query consists of exactly two clauses: SELECT\text{SELECT} and FROM\text{FROM}.
    • Structure: SELECT <column_name> FROM <table_name>;
  • Keyword Usage — DISTINCT\text{DISTINCT}:

    • Used immediately following SELECT\text{SELECT} when unique values are required from a column containing repetitive entries.
    • Application Scenario: Extracting unique manufacturer identifiers when multiple distinct products originate from the same manufacturer.
  • Wildcard Column Selection (SELECT *):

    • The asterisk symbol (*) acts as a wildcard operator denoting "all columns".
    • Selecting all records and attributes from the customer table: SELECT * FROM Customer;
    • Selecting all records and attributes from the product table: SELECT * FROM Product;
  • Query Syntax & Formatting Conventions:

    • Case Sensitivity: Column names and table names are not case-sensitive in standard SQL query execution.
    • Query Termination: Every complete SQL statement ends with a semicolon ;.
    • Multi-Query Separation: In interactive SQL windows, the semicolon serves as an execution separator, allowing multiple distinct SQL statements to sit in the same text field and run independently; for single standalone queries, the semicolon can technically be omitted, though standard practice retains it.
    • Identifier Naming Rules: Spaces are omitted from table and column names to ensure ease of query execution; multiple words within column identifiers are concatenated using camelCase or PascalCase capitalization (e.g., ProductName, ManufacturerID, ListPrice).
  • Data Display Structures:

    • Graphical Schema Representation: Displays entity structure vertically in design view.
    • Query Output Representation: Displays extracted query records horizontally, where table headers run horizontally across the top and individual dataset records populate as horizontal rows below.
  • Verbatim Sample Data Entries from Database Execution:

    • Customer Output Record Example:
    • Customer ID: 12
    • First Name: James
    • Last Name: Dovi
    • Street Address: 4 Mass Street
    • City: 1st Mouse (C B A)
    • State: Formatted as a short state code rather than a full state name.
    • Product Output Record Example:
    • Product ID: 773
    • Product Name: A book-type product entry within the database.
    • Manufacturer ID: 116
    • Composition: Formulated with leather, textile, and a rubber sole.
    • List Price: Standard catalog price field.
    • Gender: m (Male).
    • Category: boots (additional categories identified in dataset include sneakers and heels).
  • System Interface Mechanics & Grading Workflow:

    • Test Query Button: Executes query against database engine to display output preview and catch syntax errors without submitting final work.
    • Save Answer Button: Formally records query submission.
    • Practice vs. Homework System Feedback Behavior:
    • Practice Exercises: Displays feedback score along with the exact correct SQL query answer upon saving.
    • Homework Assignments: Returns feedback score only, withholding direct SQL query solution.