Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions sql-queries-11/single-row-from-join/create-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE TABLE IF NOT EXISTS countries
(
country_id integer NOT NULL,
country_name text,
CONSTRAINT countries_pkey PRIMARY KEY (country_id)
);

CREATE TABLE IF NOT EXISTS cities
(
city_id integer NOT NULL,
country_id integer,
city_name text,
population bigint,
CONSTRAINT cities_pkey PRIMARY KEY (city_id),
CONSTRAINT country_id FOREIGN KEY (country_id)
REFERENCES countries (country_id)
);

INSERT INTO countries (country_id, country_name)
VALUES
(1, 'USA'),
(2, 'UK'),
(3, 'France'),
(4, 'Thailand');

INSERT INTO cities (city_id, country_id, city_name, population)
VALUES
( 1, 1, 'Washington',689545),
( 2,1,'New York', 8804190),
( 3, 2, 'London', 12208100 ),
( 4 , 2, 'Manchester', 2732854),
( 5, 3, 'Paris', 2048472),
( 6, 3, 'Nice', 353701),
( 7, 4, 'Bangkok', 5588222),
( 8, 4, 'Phuket', 77778);
14 changes: 14 additions & 0 deletions sql-queries-11/single-row-from-join/single-row-from-join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT countries.country_id, country_name, city_id, city_name, population
FROM countries
JOIN cities ON countries.country_id = cities.country_id;

SELECT DISTINCT ON(countries.country_id)
countries.country_id, country_name, city_id, city_name, population
FROM countries
JOIN cities ON countries.country_id = cities.country_id;

SELECT DISTINCT ON(countries.country_id)
countries.country_id, country_name, city_id, city_name, population
FROM countries
JOIN cities ON countries.country_id = cities.country_id
ORDER BY countries.country_id, population DESC;