Data Warehousing
  • Data Warehousing
  • Readme
  • Fundamentals
    • Terms to Know
    • Jobs
    • Skills needed for DW developer
    • Application Tiers
    • Operational Database
    • What is a Data Warehouse
      • Typical Data Architecture
      • Problem Statement
      • Features of Data Warehouse
      • Need for Data Warehouse
      • Current State of the Art
    • Activities of Data Science
    • Types of Data
    • Data Storage Systems
    • Data Warehouse 1980 - Current
    • Data Warehouse vs Data Mart
    • Data Warehouse Architecture
      • Top-Down Approach
      • Bottom-Up Approach
    • Data Warehouse Characteristic
      • Subject Oriented
      • Integrated
      • Time Variant
      • Non Volatile
    • Tools
    • Cloud vs On-Premise
    • Steps to design a Data Warehouse
      • Gather Requirements
      • Environment
      • Data Modeling
      • Choosing ETL / ELT Solution
      • Online Analytic Processing
      • Front End
      • Query Optimization
    • Dataset Examples
    • Thoughts on some data
  • RDBMS
    • Data Model
      • Entity Relationship Model
      • Attributes
      • Keys
      • Transaction
      • ACID
    • Online vs Batch
    • DSL vs GPL
    • Connect to Elvis
    • SQL Concepts
      • Basic Select - 1
      • Basic Select - 2
      • UNION Operators
      • Wild Cards & Distinct
      • Group By & Having
      • Sub Queries
      • Derived Tables
      • Views
    • Practice using SQLBolt
  • Cloud
    • Overview
    • Types of Cloud Services
    • Challenges of Cloud Computing
    • AWS
      • AWS Global Infrastructure
      • EC2
      • S3
      • IAM
    • Terraform
  • Spark - Databricks
    • Storage Formats
    • File Formats
    • Medallion Architecture
    • Delta
  • Data Warehousing Concepts
    • Dimensional Modelling
      • Star Schema
      • Galaxy Schema
      • Snowflake Schema
      • Starflake Schema
      • Star vs Snowflake
      • GRAIN
      • Multi-Fact Star Schema
      • Vertabelo Tool
    • Dimension - Fact
    • Sample Excercise
    • Keys
      • Why Surrogate Keys are Important
    • More Examples
    • Master Data Management
    • Steps of Dimensional Modeling
    • Types of Dimensions
      • Date Dimension Table
      • Degenerate Dimension
      • Junk Dimension
      • Static Dimension
      • Conformed Dimensions
      • Slowly Changing Dimensions
        • SCD - Type 0
        • SCD - Type 1
        • SCD - Type 2
        • SCD - Type 3
        • SCD - Type 4
        • SCD - Type 6
        • SCD - Type 5 - Fun Fact
      • Role Playing Dimension
      • Conformed vs Role Playing
      • Shrunken Dimension
      • Swappable Dimension
      • Step Dimension
    • Types of Facts
      • Factless Fact Table
      • Transaction Fact
      • Periodic Fact
      • Accumulating Snapshot Fact Table
      • Transaction vs Periodic vs Accumulating
      • Additive, Semi-Additive, Non-Additive
      • Periodic Snapshot vs Additive
      • Conformed Fact
    • Sample Data Architecture Diagram
    • Data Pipeline Models
    • New DW Concepts
Powered by GitBook
On this page
  • Query with Case statements
  • Derived Tables vs Sub Query
  • Sub Query
  • Derived Table
  1. RDBMS
  2. SQL Concepts

Derived Tables

A derived table is similar to a Temporary table but derived from another SELECT statement.

When a subquery starts at the From clause (instead of Where), the result set is called Derived table.

Table invoices have around ten columns; our derived table 'tbl' has four columns.

use murach;

SELECT
    *
FROM
    (
    SELECT
        invoice_number
        ,invoice_date 
        ,payment_total 
        ,credit_total 
    FROM invoices
    ) tbl;

Query with Case statements

use Murach;

SELECT
    *
FROM
    (
    SELECT
        invoice_number
        ,invoice_date 
        ,payment_total 
        ,credit_total 
        ,CASE terms_id
            WHEN 1 THEN 'Net due 10 days'
            WHEN 2 THEN 'Net due 20 days'
            WHEN 3 THEN 'Net due 30 days'
            WHEN 4 THEN 'Net due 60 days'
            WHEN 5 THEN 'Net due 90 days'
        END AS terms
    FROM invoices
    ) tbl;

'tbl' is the Table alias name. It can be anything. Just give a name for the output of the Derived query.

Derived Tables vs Sub Query

  • When the query is used with FROM clause, it's the DERIVED table.

  • When the query is used with a WHERE clause, it's Sub Query.

Sub Query

use sakila;

SELECT 
  * 
FROM 
    actor
WHERE 
    actor_id IN 
    (SELECT actor_id FROM film_actor
    WHERE film_id = 
        (SELECT film_id FROM film 
        WHERE title = 'Ace Goldfinger')
    );

Derived Table

use sakila;

SELECT * FROM 
(
    SELECT 
        * 
    FROM 
        actor
    WHERE 
        actor_id IN 
        (SELECT actor_id FROM film_actor
        WHERE film_id = 
            (SELECT film_id FROM film 
            WHERE title = 'Ace Goldfinger')
        )
    ) t;
PreviousSub QueriesNextViews

Last updated 2 years ago