fullstack_webdev

Code and notes from Full stack web developer path, LinkedIn Learning.

View on GitHub

11. Views and Subselects

11_01 Creating a subselect

CREATE TABLE t ( a TEXT, b TEXT );
INSERT INTO t VALUES ( 'NY0123', 'US4567' );
INSERT INTO t VALUES ( 'AZ9437', 'GB1234' );
INSERT INTO t VALUES ( 'CA1279', 'FR5678' );
SELECT * FROM t;

-- Break the above table t of 3 rows and 2 columns into 4 columns of State, StateCode, Country and Country Code
SELECT SUBSTR(a, 1, 2) AS State, SUBSTR(a, 3) AS SCode,
  SUBSTR(b, 1, 2) AS Country, SUBSTR(b, 3) AS CCode FROM t;

--Use the result as data source in another select command
-- Use country codes into finding names of countries, ss is subselect
SELECT co.Name, ss.CCode FROM (
    SELECT SUBSTR(a, 1, 2) AS State, SUBSTR(a, 3) AS SCode,
      SUBSTR(b, 1, 2) AS Country, SUBSTR(b, 3) AS CCode FROM t
  ) AS ss
  JOIN Country AS co
    ON co.Code2 = ss.Country
;

SELECT * FROM Country;

DROP TABLE t;

11_02 Searching within a result set

--Say we want a list of albums that have tracks with duration of less than 90 seconds or less we do following:
SELECT DISTINCT * FROM track WHERE duration <= 90;

-- Here we print every detail of those rows where we have id is matching a subselect of a particular condition
-- We use IN operator where we have a list of values
SELECT * FROM album
  WHERE id IN (SELECT DISTINCT album_id FROM track WHERE duration <= 90)
;

SELECT a.title AS album, a.artist, t.track_number AS seq, t.title, t.duration AS secs
  FROM album AS a
  JOIN track AS t
    ON t.album_id = a.id
  WHERE a.id IN (SELECT DISTINCT album_id FROM track WHERE duration <= 90)
  ORDER BY a.title, t.track_number
;

SELECT a.title AS album, a.artist, t.track_number AS seq, t.title, t.duration AS secs
  FROM album AS a
  JOIN (
    SELECT DISTINCT album_id, track_number, duration, title
      FROM track
      WHERE duration <= 90
  ) AS t
    ON t.album_id = a.id
  ORDER BY a.title, t.track_number
;

11_03 Creating a view

-- Simple query that we want to reuse and hence wish to save as view.
SELECT id, album_id, title, track_number, duration / 60 AS m, duration % 60 AS s FROM track;

-- Create view well simply by using CREATE VIEW viewName AS <SELECT statement is the view syntax>
CREATE VIEW trackLengthInMinutes AS
    SELECT id, album_id, title, track_number,
    duration / 60 AS m,
    duration % 60 AS s
    FROM track
;

SELECT * FROM trackLengthInMinutes;

SELECT a.title AS album, a.artist, t.track_number AS seq, t.title, t.m, t.s
  FROM album AS a
  JOIN trackLengthInMinutes AS t
    ON t.album_id = a.id
  ORDER BY a.title, t.track_number
;

DROP VIEW IF EXISTS trackLengthInMinutes;

11_04 Creating a joined view

CREATE VIEW joinedAlbum AS
    SELECT a.artist AS artist,
        a.title AS album,
        t.title AS track,
        t.track_number AS trackno,
        t.duration / 60 AS m,
        t.duration % 60 AS s
        FROM track AS t
        JOIN album AS a
            ON a.id = t.album_id
;

-- Use the view with substring

SELECT artist, album, track, trackno,
  m || ':' || substr('00' || s, -2, 2) AS duration
FROM joinedAlbum;

-- Just deletes the view not the table
DROP VIEW IF EXISTS joinedAlbum;