Up until now in this tutorial we have worked with a very small and simple
table in our PostgreSQL database.
Now we want to create more tables with more content to be able to demonstrate
more database features.
Below we have listed all the SQL statements you need to create those tables, with content.
You are not required to create the tables on your own system to continue with this tutorial,
but you might better understand how PostgreSQL and SQL statements work.
Make sure you are connected to the database.
If not, follow the steps in the chapter of this tutorial.
INSERT INTO customers
INSERT INTO customers (customer_name, contact_name, address, city, postal_code, country)
VALUES
 ('Alfreds Futterkiste', 'Maria Anders', 'Obere Str.
57', 'Berlin', '12209', 'Germany'),
 ('Ana Trujillo Emparedados
y helados', 'Ana Trujillo', 'Avda. de la Constitucion 2222', 'Mexico
D.F.', '05021', 'Mexico'),
 ('Antonio Moreno Taquera', 'Antonio
Moreno', 'Mataderos 2312', 'Mexico D.F.', '05023', 'Mexico'),
ÂÂ
('Around the Horn', 'Thomas Hardy', '120 Hanover Sq.', 'London', 'WA1
1DP', 'UK'),
 ('Berglunds snabbkoep', 'Christina Berglund', 'Berguvsvegen
8', 'Lulea', 'S-958 22', 'Sweden'),
 ('Blauer See Delikatessen',
'Hanna Moos', 'Forsterstr. 57', 'Mannheim', '68306', 'Germany'),
ÂÂ
('Blondel pere et fils', 'Frederique Citeaux', '24, place Kleber',
'Strasbourg', '67000', 'France'),
 ('Bolido Comidas preparadas',
'Martin Sommer', 'C/ Araquil, 67', 'Madrid', '28023', 'Spain'),
ÂÂ
('Bon app', 'Laurence Lebihans', '12, rue des Bouchers', 'Marseille',
'13008', 'France'),
 ('Bottom-Dollar Marketse', 'Elizabeth
Lincoln', '23 Tsawassen Blvd.', 'Tsawassen', 'T2F 8M4', 'Canada'),
ÂÂ
('Bs Beverages', 'Victoria Ashworth', 'Fauntleroy Circus', 'London',
'EC2 5NT', 'UK'),
 ('Cactus Comidas para llevar', 'Patricio
Simpson', 'Cerrito 333', 'Buenos Aires', '1010', 'Argentina'),
ÂÂ
('Centro comercial Moctezuma', 'Francisco Chang', 'Sierras de Granada
9993', 'Mexico D.F.', '05022', 'Mexico'),
 ('Chop-suey
Chinese', 'Yang Wang', 'Hauptstr. 29', 'Bern', '3012', 'Switzerland'),
ÂÂ
('Comercio Mineiro', 'Pedro Afonso', 'Av. dos Lusiadas, 23', 'Sao
Paulo', '05432-043', 'Brazil'),
 ('Consolidated Holdings',
'Elizabeth Brown', 'Berkeley Gardens 12 Brewery ', 'London', 'WX1 6LT',
'UK'),
 ('Drachenblut Delikatessend', 'Sven Ottlieb', 'Walserweg
21', 'Aachen', '52066', 'Germany'),
 ('Du monde entier',
'Janine Labrune', '67, rue des Cinquante Otages', 'Nantes', '44000',
'France'),
 ('Eastern Connection', 'Ann Devon', '35 King
George', 'London', 'WX3 6FW', 'UK'),
 ('Ernst Handel', 'Roland
Mendel', 'Kirchgasse 6', 'Graz', '8010', 'Austria'),
 ('Familia
Arquibaldo', 'Aria Cruz', 'Rua Oros, 92', 'Sao Paulo', '05442-030',
'Brazil'),
 ('FISSA Fabrica Inter. Salchichas S.A.', 'Diego
Roel', 'C/ Moralzarzal, 86', 'Madrid', '28034', 'Spain'),
 ('Folies
gourmandes', 'Martine Rance', '184, chaussee de Tournai', 'Lille',
'59000', 'France'),
 ('Folk och fe HB', 'Maria Larsson', 'Akergatan
24', 'Brecke', 'S-844 67', 'Sweden'),
 ('Frankenversand',
'Peter Franken', 'Berliner Platz 43', 'Munchen', '80805', 'Germany'),
ÂÂ
('France restauration', 'Carine Schmitt', '54, rue Royale', 'Nantes',
'44000', 'France'),
 ('Franchi S.p.A.', 'Paolo Accorti', 'Via
Monte Bianco 34', 'Torino', '10100', 'Italy'),
 ('Furia
Bacalhau e Frutos do Mar', 'Lino Rodriguez ', 'Jardim das rosas n. 32',
'Lisboa', '1675', 'Portugal'),
 ('Galeria del gastronomo',
'Eduardo Saavedra', 'Rambla de Cataluna, 23', 'Barcelona', '08022',
'Spain'),
 ('Godos Cocina Tipica', 'Jose Pedro Freyre', 'C/
Romero, 33', 'Sevilla', '41101', 'Spain'),
 ('Gourmet
Lanchonetes', 'Andre Fonseca', 'Av. Brasil, 442', 'Campinas',
'04876-786', 'Brazil'),
 ('Great Lakes Food Market', 'Howard
Snyder', '2732 Baker Blvd.', 'Eugene', '97403', 'USA'),
ÂÂ
('GROSELLA-Restaurante', 'Manuel Pereira', '5th Ave. Los Palos Grandes',
'Caracas', '1081', 'Venezuela'),
 ('Hanari Carnes', 'Mario
Pontes', 'Rua do Paco, 67', 'Rio de Janeiro', '05454-876', 'Brazil'),
ÂÂ
('HILARION-Abastos', 'Carlos Hernandez', 'Carrera 22 con Ave. Carlos
Soublette #8-35', 'San Cristobal', '5022', 'Venezuela'),
ÂÂ
('Hungry Coyote Import Store', 'Yoshi Latimer', 'City Center Plaza 516
Main St.', 'Elgin', '97827', 'USA'),
 ('Hungry Owl All-Night
Grocers', 'Patricia McKenna', '8 Johnstown Road', 'Cork', '',
'Ireland'),
 ('Island Trading', 'Helen Bennett', 'Garden House
Crowther Way', 'Cowes', 'PO31 7PJ', 'UK'),
 ('Koniglich Essen',
'Philip Cramer', 'Maubelstr. 90', 'Brandenburg', '14776', 'Germany'),
ÂÂ
('La corne d abondance', 'Daniel Tonini', '67, avenue de l Europe',
'Versailles', '78000', 'France'),
 ('La maison d Asie',
'Annette Roulet', '1 rue Alsace-Lorraine', 'Toulouse', '31000',
'France'),
 ('Laughing Bacchus Wine Cellars', 'Yoshi Tannamuri',
'1900 Oak St.', 'Vancouver', 'V3F 2K1', 'Canada'),
 ('Lazy K
Kountry Store', 'John Steel', '12 Orchestra Terrace', 'Walla Walla',
'99362', 'USA'),
 ('Lehmanns Marktstand', 'Renate Messner', 'Magazinweg
7', 'Frankfurt a.M. ', '60528', 'Germany'),
 ('Lets Stop N
Shop', 'Jaime Yorres', '87 Polk St. Suite 5', 'San Francisco', '94117',
'USA'),
 ('LILA-Supermercado', 'Carlos Gonzalez', 'Carrera 52
con Ave. Bolivar #65-98 Llano Largo', 'Barquisimeto', '3508',
'Venezuela'),
 ('LINO-Delicateses', 'Felipe Izquierdo', 'Ave. 5
de Mayo Porlamar', 'I. de Margarita', '4980', 'Venezuela'),
ÂÂ
('Lonesome Pine Restaurant', 'Fran Wilson', '89 Chiaroscuro Rd.',
'Portland', '97219', 'USA'),
 ('Magazzini Alimentari Riuniti',
'Giovanni Rovelli', 'Via Ludovico il Moro 22', 'Bergamo', '24100',
'Italy'),
 ('Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens
532', 'Bruxelles', 'B-1180', 'Belgium'),
 ('Mere Paillarde',
'Jean Fresniere', '43 rue St. Laurent', 'Montreal', 'H1J 1C3',
'Canada'),
 ('Morgenstern Gesundkost', 'Alexander Feuer', 'Heerstr.
22', 'Leipzig', '04179', 'Germany'),
 ('North/South', 'Simon
Crowther', 'South House 300 Queensbridge', 'London', 'SW7 1RZ', 'UK'),
ÂÂ
('Oceano Atlantico Ltda.', 'Yvonne Moncada', 'Ing. Gustavo Moncada 8585
Piso 20-A', 'Buenos Aires', '1010', 'Argentina'),
 ('Old World
Delicatessen', 'Rene Phillips', '2743 Bering St.', 'Anchorage', '99508',
'USA'),
 ('Ottilies Keseladen', 'Henriette Pfalzheim', 'Mehrheimerstr.
369', 'Koln', '50739', 'Germany'),
 ('Paris specialites',
'Marie Bertrand', '265, boulevard Charonne', 'Paris', '75012',
'France'),
 ('Pericles Comidas clasicas', 'Guillermo
Fernandez', 'Calle Dr. Jorge Cash 321', 'Mexico D.F.', '05033',
'Mexico'),
 ('Piccolo und mehr', 'Georg Pipps', 'Geislweg 14',
'Salzburg', '5020', 'Austria'),
 ('Princesa Isabel Vinhoss',
'Isabel de Castro', 'Estrada da saude n. 58', 'Lisboa', '1756',
'Portugal'),
 ('Que Delicia', 'Bernardo Batista', 'Rua da
Panificadora, 12', 'Rio de Janeiro', '02389-673', 'Brazil'),
ÂÂ
('Queen Cozinha', 'Lucia Carvalho', 'Alameda dos Canarios, 891', 'Sao
Paulo', '05487-020', 'Brazil'),
 ('QUICK-Stop', 'Horst Kloss',
'Taucherstrasse 10', 'Cunewalde', '01307', 'Germany'),
ÂÂ
('Rancho grande', 'Sergio Gutiarrez', 'Av. del Libertador 900', 'Buenos
Aires', '1010', 'Argentina'),
 ('Rattlesnake Canyon Grocery',
'Paula Wilson', '2817 Milton Dr.', 'Albuquerque', '87110', 'USA'),
ÂÂ
('Reggiani Caseifici', 'Maurizio Moroni', 'Strada Provinciale 124',
'Reggio Emilia', '42100', 'Italy'),
 ('Ricardo Adocicados', 'Janete
Limeira', 'Av. Copacabana, 267', 'Rio de Janeiro', '02389-890',
'Brazil'),
 ('Richter Supermarkt', 'Michael Holz', 'Grenzacherweg
237', 'Genève', '1203', 'Switzerland'),
 ('Romero y tomillo',
'Alejandra Camino', 'Gran Via, 1', 'Madrid', '28001', 'Spain'),
ÂÂ
('Santa Gourmet', 'Jonas Bergulfsen', 'Erling Skakkes gate 78', 'Stavern',
'4110', 'Norway'),
 ('Save-a-lot Markets', 'Jose Pavarotti',
'187 Suffolk Ln.', 'Boise', '83720', 'USA'),
 ('Seven Seas
Imports', 'Hari Kumar', '90 Wadhurst Rd.', 'London', 'OX15 4NB', 'UK'),
ÂÂ
('Simons bistro', 'Jytte Petersen', 'Vinbeltet 34', 'Kobenhavn', '1734',
'Denmark'),
 ('Specialites du monde', 'Dominique Perrier', '25,
rue Lauriston', 'Paris', '75016', 'France'),
 ('Split Rail Beer
& Ale', 'Art Braunschweiger', 'P.O. Box 555', 'Lander', '82520', 'USA'),
ÂÂ
('Supremes delices', 'Pascale Cartrain', 'Boulevard Tirou, 255',
'Charleroi', 'B-6000', 'Belgium'),
 ('The Big Cheese', 'Liz
Nixon', '89 Jefferson Way Suite 2', 'Portland', '97201', 'USA'),
ÂÂ
('The Cracker Box', 'Liu Wong', '55 Grizzly Peak Rd.', 'Butte', '59801',
'USA'),
 ('Toms Spezialiteten', 'Karin Josephs', 'Luisenstr.
48', 'Manster', '44087', 'Germany'),
 ('Tortuga Restaurante',
'Miguel Angel Paolino', 'Avda. Azteca 123', 'Mexico D.F.', '05033',
'Mexico'),
 ('Tradicao Hipermercados', 'Anabela Domingues',
'Av. Ines de Castro, 414', 'Sao Paulo', '05634-030', 'Brazil'),
ÂÂ
('Trails Head Gourmet Provisioners', 'Helvetius Nagy', '722 DaVinci
Blvd.', 'Kirkland', '98034', 'USA'),
 ('Vaffeljernet', 'Palle
Ibsen', 'Smagsloget 45', 'Arhus', '8200', 'Denmark'),
 ('Victuailles
en stock', 'Mary Saveley', '2, rue du Commerce', 'Lyon', '69004',
'France'),
 ('Vins et alcools Chevalier', 'Paul Henriot', '59
rue de l Abbaye', 'Reims', '51100', 'France'),
 ('Die Wandernde
Kuh', 'Rita Moller', 'Adenauerallee 900', 'Stuttgart', '70563',
'Germany'),
 ('Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu
38', 'Oulu', '90110', 'Finland'),
 ('Wellington Importadora',
'Paula Parente', 'Rua do Mercado, 12', 'Resende', '08737-363',
'Brazil'),
 ('White Clover Markets', 'Karl Jablonski', '305 -
14th Ave. S. Suite 3B', 'Seattle', '98128', 'USA'),
 ('Wilman
Kala', 'Matti Karttunen', 'Keskuskatu 45', 'Helsinki', '21240',
'Finland'),
 ('Wolski', 'Zbyszek', 'ul. Filtrowa 68', 'Walla',
'01-012', 'Poland');
INSERT INTO orders
INSERT INTO orders (order_id, customer_id, order_date)
VALUES
  (10248, 90, '2021-07-04'),
  (10249, 81,
'2021-07-05'),
  (10250, 34, '2021-07-08'),
  (10251, 84,
'2021-07-08'),
  (10252, 76, '2021-07-09'),
  (10253, 34,
'2021-07-10'),
  (10254, 14, '2021-07-11'),
  (10255, 68,
'2021-07-12'),
  (10256, 88, '2021-07-15'),
  (10257, 35,
'2021-07-16'),
  (10258, 20, '2021-07-17'),
  (10259, 13,
'2021-07-18'),
  (10260, 55, '2021-07-19'),
  (10261, 61,
'2021-07-19'),
  (10262, 65, '2021-07-22'),
  (10263, 20,
'2021-07-23'),
  (10264, 24, '2021-07-24'),
  (10265, 7,
'2021-07-25'),
  (10266, 87, '2021-07-26'),
  (10267, 25,
'2021-07-29'),
  (10268, 33, '2021-07-30'),
  (10269, 89,
'2021-07-31'),
  (10270, 87, '2021-08-01'),
  (10271, 75,
'2021-08-01'),
  (10272, 65, '2021-08-02'),
  (10273, 63,
'2021-08-05'),
  (10274, 85, '2021-08-06'),
  (10275, 49,
'2021-08-07'),
  (10276, 80, '2021-08-08'),
  (10277, 52,
'2021-08-09'),
  (10278, 5, '2021-08-12'),
  (10279, 44,
'2021-08-13'),
  (10280, 5, '2021-08-14'),
  (10281, 69,
'2021-08-14'),
  (10282, 69, '2021-08-15'),
  (10283, 46,
'2021-08-16'),
  (10284, 44, '2021-08-19'),
  (10285, 63,
'2021-08-20'),
  (10286, 63, '2021-08-21'),
  (10287, 67,
'2021-08-22'),
  (10288, 66, '2021-08-23'),
  (10289, 11,
'2021-08-26'),
  (10290, 15, '2021-08-27'),
  (10291, 61,
'2021-08-27'),
  (10292, 81, '2021-08-28'),
  (10293, 80,
'2021-08-29'),
  (10294, 65, '2021-08-30'),
  (10295, 85,
'2021-09-02'),
  (10296, 46, '2021-09-03'),
  (10297, 7,
'2021-09-04'),
  (10298, 37, '2021-09-05'),
  (10299, 67,
'2021-09-06'),
  (10300, 49, '2021-09-09'),
  (10301, 86,
'2021-09-09'),
  (10302, 76, '2021-09-10'),
  (10303, 30,
'2021-09-11'),
  (10304, 80, '2021-09-12'),
  (10305, 55,
'2021-09-13'),
  (10306, 69, '2021-09-16'),
  (10307, 48,
'2021-09-17'),
  (10308, 2, '2021-09-18'),
  (10309, 37,
'2021-09-19'),
  (10310, 77, '2021-09-20'),
  (10311, 18,
'2021-09-20'),
  (10312, 86, '2021-09-23'),
  (10313, 63,
'2021-09-24'),
  (10314, 65, '2021-09-25'),
  (10315, 38,
'2021-09-26'),
  (10316, 65, '2021-09-27'),
  (10317, 48,
'2021-09-30'),
  (10318, 38, '2021-10-01'),
  (10319, 80,
'2021-10-02'),
  (10320, 87, '2021-10-03'),
  (10321, 38,
'2021-10-03'),
  (10322, 58, '2021-10-04'),
  (10323, 39,
'2021-10-07'),
  (10324, 71, '2021-10-08'),
  (10325, 39,
'2021-10-09'),
  (10326, 8, '2021-10-10'),
  (10327, 24,
'2021-10-11'),
  (10328, 28, '2021-10-14'),
  (10329, 75,
'2021-10-15'),
  (10330, 46, '2021-10-16'),
  (10331, 9,
'2021-10-16'),
  (10332, 51, '2021-10-17'),
  (10333, 87,
'2021-10-18'),
  (10334, 84, '2021-10-21'),
  (10335, 37,
'2021-10-22'),
  (10336, 60, '2021-10-23'),
  (10337, 25,
'2021-10-24'),
  (10338, 55, '2021-10-25'),
  (10339, 51,
'2021-10-28'),
  (10340, 9, '2021-10-29'),
  (10341, 73,
'2021-10-29'),
  (10342, 25, '2021-10-30'),
  (10343, 44,
'2021-10-31'),
  (10344, 89, '2021-11-01'),
  (10345, 63,
'2021-11-04'),
  (10346, 65, '2021-11-05'),
  (10347, 21,
'2021-11-06'),
  (10348, 86, '2021-11-07'),
  (10349, 75,
'2021-11-08'),
  (10350, 41, '2021-11-11'),
  (10351, 20,
'2021-11-11'),
  (10352, 28, '2021-11-12'),
  (10353, 59,
'2021-11-13'),
  (10354, 58, '2021-11-14'),
  (10355, 4,
'2021-11-15'),
  (10356, 86, '2021-11-18'),
  (10357, 46,
'2021-11-19'),
  (10358, 41, '2021-11-20'),
  (10359, 72,
'2021-11-21'),
  (10360, 7, '2021-11-22'),
  (10361, 63,
'2021-11-22'),
  (10362, 9, '2021-11-25'),
  (10363, 17,
'2021-11-26'),
  (10364, 19, '2021-11-26'),
  (10365, 3,
'2021-11-27'),
  (10366, 29, '2021-11-28'),
  (10367, 83,
'2021-11-28'),
  (10368, 20, '2021-11-29'),
  (10369, 75,
'2021-12-02'),
  (10370, 14, '2021-12-03'),
  (10371, 41,
'2021-12-03'),
  (10372, 62, '2021-12-04'),
  (10373, 37,
'2021-12-05'),
  (10374, 91, '2021-12-05'),
  (10375, 36,
'2021-12-06'),
  (10376, 51, '2021-12-09'),
  (10377, 72,
'2021-12-09'),
  (10378, 24, '2021-12-10'),
  (10379, 61,
'2021-12-11'),
  (10380, 37, '2021-12-12'),
  (10381, 46,
'2021-12-12'),
  (10382, 20, '2021-12-13'),
  (10383, 4,
'2021-12-16'),
  (10384, 5, '2021-12-16'),
  (10385, 75,
'2021-12-17'),
  (10386, 21, '2021-12-18'),
  (10387, 70,
'2021-12-18'),
  (10388, 72, '2021-12-19'),
  (10389, 10,
'2021-12-20'),
  (10390, 20, '2021-12-23'),
  (10391, 17,
'2021-12-23'),
  (10392, 59, '2021-12-24'),
  (10393, 71,
'2021-12-25'),
  (10394, 36, '2021-12-25'),
  (10395, 35,
'2021-12-26'),
  (10396, 25, '2021-12-27'),
  (10397, 60,
'2021-12-27'),
  (10398, 71, '2021-12-30'),
  (10399, 83,
'2021-12-31'),
  (10400, 19, '2022-01-01'),
  (10401, 65,
'2022-01-01'),
  (10402, 20, '2022-01-02'),
  (10403, 20,
'2022-01-03'),
  (10404, 49, '2022-01-03'),
  (10405, 47,
'2022-01-06'),
  (10406, 62, '2022-01-07'),
  (10407, 56,
'2022-01-07'),
  (10408, 23, '2022-01-08'),
  (10409, 54,
'2022-01-09'),
  (10410, 10, '2022-01-10'),
  (10411, 10,
'2022-01-10'),
  (10412, 87, '2022-01-13'),
  (10413, 41,
'2022-01-14'),
  (10414, 21, '2022-01-14'),
  (10415, 36,
'2022-01-15'),
  (10416, 87, '2022-01-16'),
  (10417, 73,
'2022-01-16'),
  (10418, 63, '2022-01-17'),
  (10419, 68,
'2022-01-20'),
  (10420, 88, '2022-01-21'),
  (10421, 61,
'2022-01-21'),
  (10422, 27, '2022-01-22'),
  (10423, 31,
'2022-01-23'),
  (10424, 51, '2022-01-23'),
  (10425, 41,
'2022-01-24'),
  (10426, 29, '2022-01-27'),
  (10427, 59,
'2022-01-27'),
  (10428, 66, '2022-01-28'),
  (10429, 37,
'2022-01-29'),
  (10430, 20, '2022-01-30'),
  (10431, 10,
'2022-01-30'),
  (10432, 75, '2022-01-31'),
  (10433, 60,
'2022-02-03'),
  (10434, 24, '2022-02-03'),
  (10435, 16,
'2022-02-04'),
  (10436, 7, '2022-02-05'),
  (10437, 87,
'2022-02-05'),
  (10438, 79, '2022-02-06'),
  (10439, 51,
'2022-02-07'),
  (10440, 71, '2022-02-10'),
  (10441, 55,
'2022-02-10'),
  (10442, 20, '2022-02-11'),
  (10443, 66,
'2022-02-12'),
  (10444, 5, '2022-02-12'),
  (10445, 5,
'2022-02-13'),
  (10446, 79, '2022-02-14'),
  (10447, 67,
'2022-02-14'),
  (10448, 64, '2022-02-17'),
  (10449, 7,
'2022-02-18'),
  (10450, 84, '2022-02-19'),
  (10451, 63,
'2022-02-19'),
  (10452, 71, '2022-02-20'),
  (10453, 4,
'2022-02-21'),
  (10454, 41, '2022-02-21'),
  (10455, 87,
'2022-02-24'),
  (10456, 39, '2022-02-25'),
  (10457, 39,
'2022-02-25'),
  (10458, 76, '2022-02-26'),
  (10459, 84,
'2022-02-27'),
  (10460, 24, '2022-02-28'),
  (10461, 46,
'2022-02-28'),
  (10462, 16, '2022-03-03'),
  (10463, 76,
'2022-03-04'),
  (10464, 28, '2022-03-04'),
  (10465, 83,
'2022-03-05'),
  (10466, 15, '2022-03-06'),
  (10467, 49,
'2022-03-06'),
  (10468, 39, '2022-03-07'),
  (10469, 89,
'2022-03-10'),
  (10470, 9, '2022-03-11'),
  (10471, 11,
'2022-03-11'),
  (10472, 72, '2022-03-12'),
  (10473, 38,
'2022-03-13'),
  (10474, 58, '2022-03-13'),
  (10475, 76,
'2022-03-14'),
  (10476, 35, '2022-03-17'),
  (10477, 60,
'2022-03-17'),
  (10478, 84, '2022-03-18'),
  (10479, 65,
'2022-03-19'),
  (10480, 23, '2022-03-20'),
  (10481, 67,
'2022-03-20'),
  (10482, 43, '2022-03-21'),
  (10483, 89,
'2022-03-24'),
  (10484, 11, '2022-03-24'),
  (10485, 47,
'2022-03-25'),
  (10486, 35, '2022-03-26'),
  (10487, 62,
'2022-03-26'),
  (10488, 25, '2022-03-27'),
  (10489, 59,
'2022-03-28'),
  (10490, 35, '2022-03-31'),
  (10491, 28,
'2022-03-31'),
  (10492, 10, '2022-04-01'),
  (10493, 41,
'2022-04-02'),
  (10494, 15, '2022-04-02'),
  (10495, 42,
'2022-04-03'),
  (10496, 81, '2022-04-04'),
  (10497, 44,
'2022-04-04'),
  (10498, 35, '2022-04-07'),
  (10499, 46,
'2022-04-08'),
  (10500, 41, '2022-04-09'),
  (10501, 6,
'2022-04-09'),
  (10502, 58, '2022-04-10'),
  (10503, 37,
'2022-04-11'),
  (10504, 89, '2022-04-11'),
  (10505, 51,
'2022-04-14'),
  (10506, 39, '2022-04-15'),
  (10507, 3,
'2022-04-15'),
  (10508, 56, '2022-04-16'),
  (10509, 6,
'2022-04-17'),
  (10510, 71, '2022-04-18'),
  (10511, 9,
'2022-04-18'),
  (10512, 21, '2022-04-21'),
  (10513, 86,
'2022-04-22'),
  (10514, 20, '2022-04-22'),
  (10515, 63,
'2022-04-23'),
  (10516, 37, '2022-04-24'),
  (10517, 53,
'2022-04-24'),
  (10518, 80, '2022-04-25'),
  (10519, 14,
'2022-04-28'),
  (10520, 70, '2022-04-29'),
  (10521, 12,
'2022-04-29'),
  (10522, 44, '2022-04-30'),
  (10523, 72,
'2022-05-01'),
  (10524, 5, '2022-05-01'),
  (10525, 9,
'2022-05-02'),
  (10526, 87, '2022-05-05'),
  (10527, 63,
'2022-05-05'),
  (10528, 32, '2022-05-06'),
  (10529, 50,
'2022-05-07'),
  (10530, 59, '2022-05-08'),
  (10531, 54,
'2022-05-08'),
  (10532, 19, '2022-05-09'),
  (10533, 24,
'2022-05-12'),
  (10534, 44, '2022-05-12'),
  (10535, 3,
'2022-05-13'),
  (10536, 44, '2022-05-14'),
  (10537, 68,
'2022-05-14'),
  (10538, 11, '2022-05-15'),
  (10539, 11,
'2022-05-16'),
  (10540, 63, '2022-05-19'),
  (10541, 34,
'2022-05-19'),
  (10542, 39, '2022-05-20'),
  (10543, 46,
'2022-05-21'),
  (10544, 48, '2022-05-21'),
  (10545, 43,
'2022-05-22'),
  (10546, 84, '2022-05-23'),
  (10547, 72,
'2022-05-23'),
  (10548, 79, '2022-05-26'),
  (10549, 63,
'2022-05-27'),
  (10550, 30, '2022-05-28'),
  (10551, 28,
'2022-05-28'),
  (10552, 35, '2022-05-29'),
  (10553, 87,
'2022-05-30'),
  (10554, 56, '2022-05-30'),
  (10555, 71,
'2022-06-02'),
  (10556, 73, '2022-06-03'),
  (10557, 44,
'2022-06-03'),
  (10558, 4, '2022-06-04'),
  (10559, 7,
'2022-06-05'),
  (10560, 25, '2022-06-06'),
  (10561, 24,
'2022-06-06'),
  (10562, 66, '2022-06-09'),
  (10563, 67,
'2022-06-10'),
  (10564, 65, '2022-06-10'),
  (10565, 51,
'2022-06-11'),
  (10566, 7, '2022-06-12'),
  (10567, 37,
'2022-06-12'),
  (10568, 29, '2022-06-13'),
  (10569, 65,
'2022-06-16'),
  (10570, 51, '2022-06-17'),
  (10571, 20,
'2022-06-17'),
  (10572, 5, '2022-06-18'),
  (10573, 3,
'2022-06-19'),
  (10574, 82, '2022-06-19'),
  (10575, 52,
'2022-06-20'),
  (10576, 80, '2022-06-23'),
  (10577, 82,
'2022-06-23'),
  (10578, 11, '2022-06-24'),
  (10579, 45,
'2022-06-25'),
  (10580, 56, '2022-06-26'),
  (10581, 21,
'2022-06-26'),
  (10582, 6, '2022-06-27'),
  (10583, 87,
'2022-06-30'),
  (10584, 7, '2022-06-30'),
  (10585, 88,
'2022-07-01'),
  (10586, 66, '2022-07-02'),
  (10587, 61,
'2022-07-02'),
  (10588, 63, '2022-07-03'),
  (10589, 32,
'2022-07-04'),
  (10590, 51, '2022-07-07'),
  (10591, 83,
'2022-07-07'),
  (10592, 44, '2022-07-08'),
  (10593, 44,
'2022-07-09'),
  (10594, 55, '2022-07-09'),
  (10595, 20,
'2022-07-10'),
  (10596, 89, '2022-07-11'),
  (10597, 59,
'2022-07-11'),
  (10598, 65, '2022-07-14'),
  (10599, 11,
'2022-07-15'),
  (10600, 36, '2022-07-16'),
  (10601, 35,
'2022-07-16'),
  (10602, 83, '2022-07-17'),
  (10603, 71,
'2022-07-18'),
  (10604, 28, '2022-07-18'),
  (10605, 51,
'2022-07-21'),
  (10606, 81, '2022-07-22'),
  (10607, 71,
'2022-07-22'),
  (10608, 79, '2022-07-23'),
  (10609, 18,
'2022-07-24'),
  (10610, 41, '2022-07-25'),
  (10611, 91,
'2022-07-25'),
  (10612, 71, '2022-07-28'),
  (10613, 35,
'2022-07-29'),
  (10614, 6, '2022-07-29'),
  (10615, 90,
'2022-07-30'),
  (10616, 32, '2022-07-31'),
  (10617, 32,
'2022-07-31'),
  (10618, 51, '2022-08-01'),
  (10619, 51,
'2022-08-04'),
  (10620, 42, '2022-08-05'),
  (10621, 38,
'2022-08-05'),
  (10622, 67, '2022-08-06'),
  (10623, 25,
'2022-08-07'),
  (10624, 78, '2022-08-07'),
  (10625, 2,
'2022-08-08'),
  (10626, 5, '2022-08-11'),
  (10627, 71,
'2022-08-11'),
  (10628, 7, '2022-08-12'),
  (10629, 30,
'2022-08-12'),
  (10630, 39, '2022-08-13'),
  (10631, 41,
'2022-08-14'),
  (10632, 86, '2022-08-14'),
  (10633, 20,
'2022-08-15'),
  (10634, 23, '2022-08-15'),
  (10635, 49,
'2022-08-18'),
  (10636, 87, '2022-08-19'),
  (10637, 62,
'2022-08-19'),
  (10638, 47, '2022-08-20'),
  (10639, 70,
'2022-08-20'),
  (10640, 86, '2022-08-21'),
  (10641, 35,
'2022-08-22'),
  (10642, 73, '2022-08-22'),
  (10643, 1,
'2022-08-25'),
  (10644, 88, '2022-08-25'),
  (10645, 34,
'2022-08-26'),
  (10646, 37, '2022-08-27'),
  (10647, 61,
'2022-08-27'),
  (10648, 67, '2022-08-28'),
  (10649, 50,
'2022-08-28'),
  (10650, 21, '2022-08-29'),
  (10651, 86,
'2022-09-01'),
  (10652, 31, '2022-09-01'),
  (10653, 25,
'2022-09-02'),
  (10654, 5, '2022-09-02'),
  (10655, 66,
'2022-09-03'),
  (10656, 32, '2022-09-04'),
  (10657, 71,
'2022-09-04'),
  (10658, 63, '2022-09-05'),
  (10659, 62,
'2022-09-05'),
  (10660, 36, '2022-09-08'),
  (10661, 37,
'2022-09-09'),
  (10662, 48, '2022-09-09'),
  (10663, 9,
'2022-09-10'),
  (10664, 28, '2022-09-10'),
  (10665, 48,
'2022-09-11'),
  (10666, 68, '2022-09-12'),
  (10667, 20,
'2022-09-12'),
  (10668, 86, '2022-09-15'),
  (10669, 73,
'2022-09-15'),
  (10670, 25, '2022-09-16'),
  (10671, 26,
'2022-09-17'),
  (10672, 5, '2022-09-17'),
  (10673, 90,
'2022-09-18'),
  (10674, 38, '2022-09-18'),
  (10675, 25,
'2022-09-19'),
  (10676, 80, '2022-09-22'),
  (10677, 3,
'2022-09-22'),
  (10678, 71, '2022-09-23'),
  (10679, 7,
'2022-09-23'),
  (10680, 55, '2022-09-24'),
  (10681, 32,
'2022-09-25'),
  (10682, 3, '2022-09-25'),
  (10683, 18,
'2022-09-26'),
  (10684, 56, '2022-09-26'),
  (10685, 31,
'2022-09-29'),
  (10686, 59, '2022-09-30'),
  (10687, 37,
'2022-09-30'),
  (10688, 83, '2022-10-01'),
  (10689, 5,
'2022-10-01'),
  (10690, 34, '2022-10-02'),
  (10691, 63,
'2022-10-03'),
  (10692, 1, '2022-10-03'),
  (10693, 89,
'2022-10-06'),
  (10694, 63, '2022-10-06'),
  (10695, 90,
'2022-10-07'),
  (10696, 89, '2022-10-08'),
  (10697, 47,
'2022-10-08'),
  (10698, 20, '2022-10-09'),
  (10699, 52,
'2022-10-09'),
  (10700, 71, '2022-10-10'),
  (10701, 37,
'2022-10-13'),
  (10702, 1, '2022-10-13'),
  (10703, 24,
'2022-10-14'),
  (10704, 62, '2022-10-14'),
  (10705, 35,
'2022-10-15'),
  (10706, 55, '2022-10-16'),
  (10707, 4,
'2022-10-16'),
  (10708, 77, '2022-10-17'),
  (10709, 31,
'2022-10-17'),
  (10710, 27, '2022-10-20'),
  (10711, 71,
'2022-10-21'),
  (10712, 37, '2022-10-21'),
  (10713, 71,
'2022-10-22'),
  (10714, 71, '2022-10-22'),
  (10715, 9,
'2022-10-23'),
  (10716, 64, '2022-10-24'),
  (10717, 25,
'2022-10-24'),
  (10718, 39, '2022-10-27'),
  (10719, 45,
'2022-10-27'),
  (10720, 61, '2022-10-28'),
  (10721, 63,
'2022-10-29'),
  (10722, 71, '2022-10-29'),
  (10723, 89,
'2022-10-30'),
  (10724, 51, '2022-10-30'),
  (10725, 21,
'2022-10-31'),
  (10726, 19, '2022-11-03'),
  (10727, 66,
'2022-11-03'),
  (10728, 62, '2022-11-04'),
  (10729, 47,
'2022-11-04'),
  (10730, 9, '2022-11-05'),
  (10731, 14,
'2022-11-06'),
  (10732, 9, '2022-11-06'),
  (10733, 5,
'2022-11-07'),
  (10734, 31, '2022-11-07'),
  (10735, 45,
'2022-11-10'),
  (10736, 37, '2022-11-11'),
  (10737, 85,
'2022-11-11'),
  (10738, 74, '2022-11-12'),
  (10739, 85,
'2022-11-12'),
  (10740, 89, '2022-11-13'),
  (10741, 4,
'2022-11-14'),
  (10742, 10, '2022-11-14'),
  (10743, 4,
'2022-11-17'),
  (10744, 83, '2022-11-17'),
  (10745, 63,
'2022-11-18'),
  (10746, 14, '2022-11-19'),
  (10747, 59,
'2022-11-19'),
  (10748, 71, '2022-11-20'),
  (10749, 38,
'2022-11-20'),
  (10750, 87, '2022-11-21'),
  (10751, 68,
'2022-11-24'),
  (10752, 53, '2022-11-24'),
  (10753, 27,
'2022-11-25'),
  (10754, 49, '2022-11-25'),
  (10755, 9,
'2022-11-26'),
  (10756, 75, '2022-11-27'),
  (10757, 71,
'2022-11-27'),
  (10758, 68, '2022-11-28'),
  (10759, 2,
'2022-11-28'),
  (10760, 50, '2022-12-01'),
  (10761, 65,
'2022-12-02'),
  (10762, 24, '2022-12-02'),
  (10763, 23,
'2022-12-03'),
  (10764, 20, '2022-12-03'),
  (10765, 63,
'2022-12-04'),
  (10766, 56, '2022-12-05'),
  (10767, 76,
'2022-12-05'),
  (10768, 4, '2022-12-08'),
  (10769, 83,
'2022-12-08'),
  (10770, 34, '2022-12-09'),
  (10771, 20,
'2022-12-10'),
  (10772, 44, '2022-12-10'),
  (10773, 20,
'2022-12-11'),
  (10774, 24, '2022-12-11'),
  (10775, 78,
'2022-12-12'),
  (10776, 20, '2022-12-15'),
  (10777, 31,
'2022-12-15'),
  (10778, 5, '2022-12-16'),
  (10779, 52,
'2022-12-16'),
  (10780, 46, '2022-12-16'),
  (10781, 87,
'2022-12-17'),
  (10782, 12, '2022-12-17'),
  (10783, 34,
'2022-12-18'),
  (10784, 49, '2022-12-18'),
  (10785, 33,
'2022-12-18'),
  (10786, 62, '2022-12-19'),
  (10787, 41,
'2022-12-19'),
  (10788, 63, '2022-12-22'),
  (10789, 23,
'2022-12-22'),
  (10790, 31, '2022-12-22'),
  (10791, 25,
'2022-12-23'),
  (10792, 91, '2022-12-23'),
  (10793, 4,
'2022-12-24'),
  (10794, 61, '2022-12-24'),
  (10795, 20,
'2022-12-24'),
  (10796, 35, '2022-12-25'),
  (10797, 17,
'2022-12-25'),
  (10798, 38, '2022-12-26'),
  (10799, 39,
'2022-12-26'),
  (10800, 72, '2022-12-26'),
  (10801, 8,
'2022-12-29'),
  (10802, 73, '2022-12-29'),
  (10803, 88,
'2022-12-30'),
  (10804, 72, '2022-12-30'),
  (10805, 77,
'2022-12-30'),
  (10806, 84, '2022-12-31'),
  (10807, 27,
'2022-12-31'),
  (10808, 60, '2023-01-01'),
  (10809, 88,
'2023-01-01'),
  (10810, 42, '2023-01-01'),
  (10811, 47,
'2023-01-02'),
  (10812, 66, '2023-01-02'),
  (10813, 67,
'2023-01-05'),
  (10814, 84, '2023-01-05'),
  (10815, 71,
'2023-01-05'),
  (10816, 32, '2023-01-06'),
  (10817, 39,
'2023-01-06'),
  (10818, 49, '2023-01-07'),
  (10819, 12,
'2023-01-07'),
  (10820, 65, '2023-01-07'),
  (10821, 75,
'2023-01-08'),
  (10822, 82, '2023-01-08'),
  (10823, 46,
'2023-01-09'),
  (10824, 24, '2023-01-09'),
  (10825, 17,
'2023-01-09'),
  (10826, 7, '2023-01-12'),
  (10827, 9,
'2023-01-12'),
  (10828, 64, '2023-01-13'),
  (10829, 38,
'2023-01-13'),
  (10830, 81, '2023-01-13'),
  (10831, 70,
'2023-01-14'),
  (10832, 41, '2023-01-14'),
  (10833, 56,
'2023-01-15'),
  (10834, 81, '2023-01-15'),
  (10835, 1,
'2023-01-15'),
  (10836, 20, '2023-01-16'),
  (10837, 5,
'2023-01-16'),
  (10838, 47, '2023-01-19'),
  (10839, 81,
'2023-01-19'),
  (10840, 47, '2023-01-19'),
  (10841, 76,
'2023-01-20'),
  (10842, 80, '2023-01-20'),
  (10843, 84,
'2023-01-21'),
  (10844, 59, '2023-01-21'),
  (10845, 63,
'2023-01-21'),
  (10846, 76, '2023-01-22'),
  (10847, 71,
'2023-01-22'),
  (10848, 16, '2023-01-23'),
  (10849, 39,
'2023-01-23'),
  (10850, 84, '2023-01-23'),
  (10851, 67,
'2023-01-26'),
  (10852, 65, '2023-01-26'),
  (10853, 6,
'2023-01-27'),
  (10854, 20, '2023-01-27'),
  (10855, 55,
'2023-01-27'),
  (10856, 3, '2023-01-28'),
  (10857, 5,
'2023-01-28'),
  (10858, 40, '2023-01-29'),
  (10859, 25,
'2023-01-29'),
  (10860, 26, '2023-01-29'),
  (10861, 89,
'2023-01-30'),
  (10862, 44, '2023-01-30'),
  (10863, 35,
'2023-02-02'),
  (10864, 4, '2023-02-02'),
  (10865, 63,
'2023-02-02'),
  (10866, 5, '2023-02-03'),
  (10867, 48,
'2023-02-03'),
  (10868, 62, '2023-02-04'),
  (10869, 72,
'2023-02-04'),
  (10870, 91, '2023-02-04'),
  (10871, 9,
'2023-02-05'),
  (10872, 30, '2023-02-05'),
  (10873, 90,
'2023-02-06'),
  (10874, 30, '2023-02-06'),
  (10875, 5,
'2023-02-06'),
  (10876, 9, '2023-02-09'),
  (10877, 67,
'2023-02-09'),
  (10878, 63, '2023-02-10'),
  (10879, 90,
'2023-02-10'),
  (10880, 24, '2023-02-10'),
  (10881, 12,
'2023-02-11'),
  (10882, 71, '2023-02-11'),
  (10883, 48,
'2023-02-12'),
  (10884, 45, '2023-02-12'),
  (10885, 76,
'2023-02-12'),
  (10886, 34, '2023-02-13'),
  (10887, 29,
'2023-02-13'),
  (10888, 30, '2023-02-16'),
  (10889, 65,
'2023-02-16'),
  (10890, 18, '2023-02-16'),
  (10891, 44,
'2023-02-17'),
  (10892, 50, '2023-02-17'),
  (10893, 39,
'2023-02-18'),
  (10894, 71, '2023-02-18'),
  (10895, 20,
'2023-02-18'),
  (10896, 50, '2023-02-19'),
  (10897, 37,
'2023-02-19'),
  (10898, 54, '2023-02-20'),
  (10899, 46,
'2023-02-20'),
  (10900, 88, '2023-02-20'),
  (10901, 35,
'2023-02-23'),
  (10902, 24, '2023-02-23'),
  (10903, 34,
'2023-02-24'),
  (10904, 89, '2023-02-24'),
  (10905, 88,
'2023-02-24'),
  (10906, 91, '2023-02-25'),
  (10907, 74,
'2023-02-25'),
  (10908, 66, '2023-02-26'),
  (10909, 70,
'2023-02-26'),
  (10910, 90, '2023-02-26'),
  (10911, 30,
'2023-02-26'),
  (10912, 37, '2023-02-26'),
  (10913, 62,
'2023-02-26'),
  (10914, 62, '2023-02-27'),
  (10915, 80,
'2023-02-27'),
  (10916, 64, '2023-02-27'),
  (10917, 69,
'2023-03-02'),
  (10918, 10, '2023-03-02'),
  (10919, 47,
'2023-03-02'),
  (10920, 4, '2023-03-03'),
  (10921, 83,
'2023-03-03'),
  (10922, 34, '2023-03-03'),
  (10923, 41,
'2023-03-03'),
  (10924, 5, '2023-03-04'),
  (10925, 34,
'2023-03-04'),
  (10926, 2, '2023-03-04'),
  (10927, 40,
'2023-03-05'),
  (10928, 29, '2023-03-05'),
  (10929, 25,
'2023-03-05'),
  (10930, 76, '2023-03-06'),
  (10931, 68,
'2023-03-06'),
  (10932, 9, '2023-03-06'),
  (10933, 38,
'2023-03-06'),
  (10934, 44, '2023-03-09'),
  (10935, 88,
'2023-03-09'),
  (10936, 32, '2023-03-09'),
  (10937, 12,
'2023-03-10'),
  (10938, 63, '2023-03-10'),
  (10939, 49,
'2023-03-10'),
  (10940, 9, '2023-03-11'),
  (10941, 71,
'2023-03-11'),
  (10942, 66, '2023-03-11'),
  (10943, 11,
'2023-03-11'),
  (10944, 10, '2023-03-12'),
  (10945, 52,
'2023-03-12'),
  (10946, 83, '2023-03-12'),
  (10947, 11,
'2023-03-13'),
  (10948, 30, '2023-03-13'),
  (10949, 10,
'2023-03-13'),
  (10950, 49, '2023-03-16'),
  (10951, 68,
'2023-03-16'),
  (10952, 1, '2023-03-16'),
  (10953, 4,
'2023-03-16'),
  (10954, 47, '2023-03-17'),
  (10955, 24,
'2023-03-17'),
  (10956, 6, '2023-03-17'),
  (10957, 35,
'2023-03-18'),
  (10958, 54, '2023-03-18'),
  (10959, 31,
'2023-03-18'),
  (10960, 35, '2023-03-19'),
  (10961, 62,
'2023-03-19'),
  (10962, 63, '2023-03-19'),
  (10963, 28,
'2023-03-19'),
  (10964, 74, '2023-03-20'),
  (10965, 55,
'2023-03-20'),
  (10966, 14, '2023-03-20'),
  (10967, 79,
'2023-03-23'),
  (10968, 20, '2023-03-23'),
  (10969, 15,
'2023-03-23'),
  (10970, 8, '2023-03-24'),
  (10971, 26,
'2023-03-24'),
  (10972, 40, '2023-03-24'),
  (10973, 40,
'2023-03-24'),
  (10974, 75, '2023-03-25'),
  (10975, 10,
'2023-03-25'),
  (10976, 35, '2023-03-25'),
  (10977, 24,
'2023-03-26'),
  (10978, 50, '2023-03-26'),
  (10979, 20,
'2023-03-26'),
  (10980, 24, '2023-03-27'),
  (10981, 34,
'2023-03-27'),
  (10982, 10, '2023-03-27'),
  (10983, 71,
'2023-03-27'),
  (10984, 71, '2023-03-30'),
  (10985, 37,
'2023-03-30'),
  (10986, 54, '2023-03-30'),
  (10987, 19,
'2023-03-31'),
  (10988, 65, '2023-03-31'),
  (10989, 61,
'2023-03-31'),
  (10990, 20, '2023-04-01'),
  (10991, 63,
'2023-04-01'),
  (10992, 77, '2023-04-01'),
  (10993, 24,
'2023-04-01'),
  (10994, 83, '2023-04-02'),
  (10995, 58,
'2023-04-02'),
  (10996, 63, '2023-04-02'),
  (10997, 46,
'2023-04-03'),
  (10998, 91, '2023-04-03'),
  (10999, 56,
'2023-04-03'),
  (11000, 65, '2023-04-06'),
  (11001, 24,
'2023-04-06'),
  (11002, 71, '2023-04-06'),
  (11003, 78,
'2023-04-06'),
  (11004, 50, '2023-04-07'),
  (11005, 90,
'2023-04-07'),
  (11006, 32, '2023-04-07'),
  (11007, 60,
'2023-04-08'),
  (11008, 20, '2023-04-08'),
  (11009, 30,
'2023-04-08'),
  (11010, 66, '2023-04-09'),
  (11011, 1,
'2023-04-09'),
  (11012, 25, '2023-04-09'),
  (11013, 69,
'2023-04-09'),
  (11014, 47, '2023-04-10'),
  (11015, 70,
'2023-04-10'),
  (11016, 4, '2023-04-10'),
  (11017, 20,
'2023-04-13'),
  (11018, 48, '2023-04-13'),
  (11019, 64,
'2023-04-13'),
  (11020, 56, '2023-04-14'),
  (11021, 63,
'2023-04-14'),
  (11022, 34, '2023-04-14'),
  (11023, 11,
'2023-04-14'),
  (11024, 19, '2023-04-15'),
  (11025, 87,
'2023-04-15'),
  (11026, 27, '2023-04-15'),
  (11027, 10,
'2023-04-16'),
  (11028, 39, '2023-04-16'),
  (11029, 14,
'2023-04-16'),
  (11030, 71, '2023-04-17'),
  (11031, 71,
'2023-04-17'),
  (11032, 89, '2023-04-17'),
  (11033, 68,
'2023-04-17'),
  (11034, 55, '2023-04-20'),
  (11035, 76,
'2023-04-20'),
ÂÂ
Operators in the WHERE clause
We can operate with different operators in the WHERE
clause:
= |
Equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
<> |
Not equal to |
!= |
Not equal to |
LIKE |
Check if a value matches a pattern (case sensitive) |
ILIKE |
Check if a value matches a pattern (case
insensitive) |
AND |
Logical AND |
OR |
Logical OR |
IN |
Check if a value is between a range of values |
BETWEEN |
Check if a value is between a range of values |
IS NULL |
Check if a value is NULL |
NOT |
Makes a negative result e.g.
NOT LIKE , NOT IN ,
NOT BETWEEN
|
Equal To
The =
operator is used when you want to return all records where a
column is equal to a specified value:
Example
Return all records where the brand is 'Volvo':
SELECT * FROM cars
WHERE brand = 'Volvo';
Less Than
The <
operator is used when you want to return all records where a
column is less than a specified value.
Example
Return all records where the year is less than 1975:
SELECT * FROM cars
WHERE model < 1975;
Greater Than
The >
operator is used when you want to return all records where a
columns is greater than a specified value.
Example
Return all records where the year is greater than 1975:
SELECT * FROM cars
WHERE model > 1975;
Less Than or Equal To
The <=
operator is used when you want to return all records where a
column is less than, or equal to, a specified value.
Example
Return all records where the year is less than or equal to 1975:
SELECT * FROM cars
WHERE model <= 1975;
Greater Than or Equal to
The >=
operator is used when you want to return all records where a
columns is greater than, or equal to, a specified value.
Example
Return all records where the year is greater than or equal 1975:
SELECT * FROM cars
WHERE model >= 1975;
Not Equal To
The <>
operator is used when you want to return all records where a
column is NOT equal to a specified value:
Example
Return all records where the brand is NOT 'Volvo':
SELECT * FROM cars
WHERE brand <> 'Volvo';
You will get the same reult with the !=
operator:
Example
Return all records where the brand is NOT 'Volvo':
SELECT * FROM cars
WHERE brand != 'Volvo';
LIKE
The LIKE
operator is used when you want to return all records where a
column is equal to a specified pattern.
The pattern can be an absolute value like 'Volvo', or with a wildcard that has a special meaning.
There are two wildcards often used in conjunction with the LIKE operator:
- The percent sign
%
, represents zero, one, or multiple characters.
- The underscore sign
_
, represents one single character.
Example
Return all records where the model STARTS with a capital 'M':
SELECT * FROM cars
WHERE model LIKE 'M%';
The LIKE
operator is case sensitive.
ILIKE
Same as the LIKE
operator, but
ILIKE
is case insensitive.
Example
Return all records where the model start with a 'm':
SELECT * FROM cars
WHERE model ILIKE 'm%';
AND
The logical AND
operator is used when you want to
check more that one condition:
Example
Return all records where the brand is 'Volvo' and the year is 1968:
SELECT * FROM cars
WHERE brand = 'Volvo' AND year = 1968;
OR
The logical OR
operator is used when you can accept that only one of many
conditions is true:
Example
Return all records where the brand is 'Volvo' OR the year is 1975:
SELECT * FROM cars
WHERE brand = 'Volvo' OR year = 1975;
IN
The IN
operator is used when a column's value matches any of the values in a list:
Example
Return all records where the brand is present in this list: ('Volvo', 'Mercedes', 'Ford'):
SELECT * FROM cars
WHERE brand IN ('Volvo', 'Mercedes', 'Ford');
BETWEEN
The BETWEEN
operator is used to check if a column's value
is between a specified range of values:
Example
Return all records where the year is between 1970 and 1980:
SELECT * FROM cars
WHERE year BETWEEN 1970 AND 1980;
The BETWEEN
operator includes the
from
and
to
values, meaning that in the above example,
the result would include cars made in 1970 and 1980 as well.
IS NULL
The IS NULL
operator is used to check if a column's value
is NULL:
Example
Return all records where the model is NULL:
SELECT * FROM cars
WHERE model IS NULL;
NOT
The NOT
operator can be used together with
LIKE
, ILIKE
,
IN
, BETWEEN
, and
NULL
operators to reverse the truth of the operator.
Example: NOT LIKE
Return all records where the brand does NOT start with a capital 'B' (case sensitive):
SELECT * FROM cars
WHERE brand
NOT LIKE 'B%';
Example: NOT ILIKE
Return all records where the brand does NOT start with a 'b' (case
insensitive):
SELECT * FROM cars
WHERE brand
NOT ILIKE 'b%';
Example: NOT IN
Return all records where the brand is NOT present in this list: ('Volvo', 'Mercedes', 'Ford'):
SELECT * FROM cars
WHERE brand NOT IN ('Volvo', 'Mercedes', 'Ford');
Example: NOT BETWEEN
Return all records where the year is NOT between 1970 and 1980:
SELECT * FROM cars
WHERE year NOT BETWEEN 1970 AND 1980;
The NOT BETWEEN
operator excludes the
from
and
to
values, meaning that in the above example,
the result would not include cars made in 1970 and 1980.
Example: IS NOT NULL
Return all records where the model is NOT null:
SELECT * FROM cars
WHERE model IS NOT NULL;
The cars
table has no columns with NULL values,
so the example above will return all 4 rows.
Select Data
To retrieve data from a data base, we use the
SELECT
statement.
Specify Columns
By specifying the column names, we can choose which columns to select:
Example
SELECT
customer_name, country FROM customers;
Return ALL Columns
Specify a *
instead of the column names to select all columns:
Example
SELECT * FROM customers;
The SELECT DISTINCT Statement
The SELECT DISTINCT
statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values
and sometimes you only want to list the different (distinct) values.
Example
Select only the DISTINCT values from the country
column in the customers
table:
SELECT DISTINCT country FROM customers;
Even though the customers table has 91 records, it only has 21 different countries, and that is what you get as a
result when executing the SELECT DISTINCT
statement above
SELECT COUNT(DISTINCT)
We can also use the DISTINCT
keyword in combination with the
COUNT
statement, which in the example below will return the number of
different countries there are in the customers
table.
Example
Return the number of different countries there are in the customers
table:
SELECT COUNT(DISTINCT country) FROM customers;
Filter Records
The WHERE
clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
If we want to return only the records where city
is
London
,
we can specify that in the WHERE
clause:
Example
SELECT * FROM customers
WHERE
city = 'London';
Text Fields vs. Numeric Fields
PostgreSQL requires quotes around text values.
However, numeric fields should not be enclosed in quotes:
Example
SELECT * FROM customers
WHERE
customer_id = 19;
Quotes around numeric fields will not fail, but it is good practice to always write numeric values without quotes.
Greater than
Use the >
operator to return all records where
customer_id
is greater than 80:
Example
SELECT * FROM customers
WHERE
customer_id > 80;
Quotes around numeric fields will not fail, but it is good practice to always write numeric values without quotes.
Sort Data
The ORDER BY
keyword is used to sort the result in ascending or descending order.
The ORDER BY
keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC
keyword.
Example
Sort the table by year:
SELECT * FROM products
ORDER BY price;
DESC
The ORDER BY
keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC
keyword.
Example
Sort the table by year, in descending order:
SELECT * FROM products
ORDER BY price DESC;
Sort Alphabetically
For string values the ORDER BY
keyword will order alphabetically:
Example
Sort the table by brand:
SELECT * FROM products
ORDER BY product_name;
Alphabetically DESC
To sort the table reverse alphabetically, use the DESC
keyword:
Example
Sort the table by brand, in descending order:
SELECT * FROM products
ORDER BY product_name DESC;
The LIMIT Clause
The LIMIT
clause is used to limit the maximum number of records to return.
Example
Return only the 20 first records from the customers
table:
SELECT * FROM customers
LIMIT 20;
The OFFSET Clause
The OFFSET
clause is used to specify where to start selecting the records to return.
If you want to return 20 records, but start at number 40, you can use both
LIMIT
and OFFSET
.
Note: The first record is number
0
, so when you specify
OFFSET 40
it means starting at record number 41.
Example
Return 20 records, starting from the 41th record:
SELECT * FROM customers
LIMIT 20 OFFSET 40;
MIN
The MIN()
function returns the smallest value of the selected column.
Example
Return the lowest price in the products
table:
SELECT MIN(price)
FROM products;
MAX
The MAX()
function returns the largest value of the selected column.
Example
Return the highest price in the products
table:
SELECT MAX(price)
FROM products;
Set Column Name
When you use MIN()
or MAX()
,
the returned column will be named
min
or max
by default.
To give the column a new name, use the AS
keyword.
Example
Return the lowest price, and name the column lowest_price
:
SELECT MIN(price) AS lowest_price
FROM products;
COUNT
The COUNT()
function returns the number of rows that matches a specified criterion.
If the specified criterion is a column name,
the COUNT()
function returns the number of columns with that name.
Example
Return the number of customers from the customers
table:
SELECT COUNT(customer_id)
FROM customers;
Note: NULL values are not counted.
By specifying a WHERE
clause, you can e.g. return the number of customers
that comes from London:
Example
Return the number of customers from London:
SELECT COUNT(customer_id)
FROM customers
WHERE city = 'London';
SUM
The SUM()
function returns the total sum of a numeric column.
The following SQL statement finds the sum of the
quantity
fields in the
order_details
table:
Example
Return the total amount of ordered items:
SELECT SUM(quantity)
FROM order_details;
Note: NULL values are ignored.
AVG
The AVG()
function returns the average value of a numeric column.
Example
Return the average price of all the products in the products
table:
SELECT AVG(price)
FROM products;
Note: NULL values are ignored.
With 2 Decimals
The above example returned the average price of all products, the result was
28.8663636363636364
.
We can use the ::NUMERIC
operator to round the average price to a number with 2 decimals:
Example
Return the average price of all the products, rounded to 2 decimals:
SELECT AVG(price)::NUMERIC(10,2)
FROM products;
LIKE
The LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE
operator:
-
%
The percent sign represents zero, one, or multiple characters
-
_
The underscore sign represents one, single character
Starts with
To return records that starts with a specific letter or phrase, add the %
at the end of the letter or phrase.
Example
Return all customers with a name that starts with the letter 'A':
SELECT * FROM customers
WHERE customer_name LIKE 'A%';
Contains
To return records that contains a specific letter or phrase, add the %
both before and after the letter or phrase.
Example
Return all customers with a name that contains the letter 'A':
SELECT * FROM customers
WHERE customer_name LIKE '%A%';
ILIKE
Note: The LIKE
operator is case sensitive,
if you want to do a case insensitive search, use the ILIKE
operator instead.
Example
Return all customers with a name that contains the letter 'A' or 'a':
SELECT * FROM customers
WHERE customer_name ILIKE '%A%';
Ends with
To return records that ends with a specific letter or phrase,
add the %
before the letter or phrase.
Example
Return all customers with a name that ends with the phrase 'en':
SELECT * FROM customers
WHERE customer_name LIKE '%en';
The Undescore _
Wildcard
The _
wildcard represents a single character.
It can be any character or number, but each _
represents one, and only one, character.
Example
Return all customers from a city that starts with 'L' followed by one wildcard character, then 'nd' and then two wildcard characters:
SELECT * FROM customers
WHERE city LIKE 'L_nd__';
IN
The IN
operator allows you to specify a list of possible values in the WHERE clause.
The IN
operator is a shorthand for multiple OR conditions.
Example
Return all customers FROM 'Germany', France' or 'UK':
SELECT * FROM customers
WHERE country IN ('Germany', 'France', 'UK');
NOT IN
By using the NOT
keyword in front of the IN
operator, you return all records that are NOT any of the values in the list.
Example
Return all customers that are NOT from 'Germany', France' or 'UK':
SELECT * FROM customers
WHERE country NOT IN ('Germany', 'France', 'UK');
IN (SELECT)
You can also us a SELECT
statement inside the parenthesis to return all records
that are in the result of the SELECT
statement.
Example
Return all customers that have an order in the orders
table:
SELECT * FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders);
NOT IN (SELECT)
The result in the example above returned 89 records, that means that there are 2 customers that
haven't placed any orders.
Let us check if that is correct, by using the NOT IN
operator.
Example
Return all customers that have NOT placed any orders in the orders
table:
SELECT * FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);
BETWEEN
The BETWEEN
operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN
operator is inclusive: begin and end values are included.
Example
Select all products with a price between 10 and 15:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 15;
BETWEEN Text Values
The BETWEEN
operator can also be used on text values.
The result returns all records that are alphabetically between the specified values.
Example
Select all products between 'Pavlova' and 'Tofu':
SELECT * FROM Products
WHERE product_name BETWEEN 'Pavlova' AND 'Tofu';
If we add an ORDER BY
clause to the example above, it will be a bit easier to read:
Example
Same example as above, but we sort it by product_name
:
SELECT * FROM Products
WHERE product_name BETWEEN 'Pavlova' AND 'Tofu'
ORDER BY product_name;
BETWEEN Date Values
The BETWEEN
operator can also be used on date values.
Example
Select all orders between 12. of April 2023 and 5. of May 20203:
SELECT * FROM orders
WHERE order_date BETWEEN '2023-04-12' AND '2023-05-05';
Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS
keyword.
Example
Using aliases for columns:
SELECT customer_id AS id
FROM customers;
AS is Optional
Actually, you can skip the AS
keyword and get the same result:
Example
Same result without AS
:
SELECT customer_id id
FROM customers;
Concatenate Columns
The AS
keyword is often used when two or more fields are concatenated into one.
To concatenate two fields use ||
.
Example
Concatenate two fields and call them product
:
SELECT product_name
|| unit AS product
FROM products;
Note: In the result of the example above we are missing a space between
product_name and unit. To add a space when concatenating, use || ' ' ||
.
Example
Concatenate, with space:
SELECT product_name || ' ' || unit AS product
FROM products;
Using Aliases With a Space Character
If you want your alias to contain one or more spaces, like "My Great Products
", surround your alias with double quotes.
Example
Surround your alias with double quotes:
SELECT product_name AS "My Great Products"
FROM products;
JOIN
A JOIN
clause is used to combine rows from two or
more tables, based on a related column between them.
Let's look at a selection from the products
table:
 product_id | product_name | category_id
------------+----------------+-------------
       ÂÂ
33 | Geitost      ÂÂ
|          4
       ÂÂ
34 | Sasquatch
Ale |          1
       ÂÂ
35 | Steeleye
Stout |          1
       ÂÂ
36 | Inlagd
Sill   |         ÂÂ
8
Then, look at a selection from the categories
table:
 category_id | category_name
-------------+----------------
         ÂÂ
1 | Beverages
         ÂÂ
2 | Condiments
         ÂÂ
3 | Confections
         ÂÂ
4 | Dairy Products
Notice that the category_id
column in the
products
table refers to the
category_id
in the
categories
table.
The relationship between the two tables above is the category_id
column.
Then, we can create the following SQL statement (with a JOIN),
that selects records that have matching values in both tables:
Example
Join products
to
categories
using the category_id
column:
SELECT product_id, product_name, category_name
FROM products
INNER JOIN categories ON products.category_id = categories.category_id;
If we pull out the same selection from products table above, we get this result:
Result
 product_id
| product_name | category_name
------------+----------------+----------------
       ÂÂ
33 | Geitost       | Dairy Products
       ÂÂ
34 | Sasquatch Ale | Beverages
       ÂÂ
35 | Steeleye Stout | Beverages
       ÂÂ
36 | Inlagd Sill   | Seafood
Different Types of Joins
Here are the different types of the Joins in PostgreSQL:
-
INNER JOIN
: Returns records that have matching values in both tables
-
LEFT JOIN
: Returns all records from the left table, and the matched records from the right table
-
RIGHT JOIN
: Returns all records from the right table, and the matched records from the left table
-
FULL JOIN
: Returns all records when there is a match in either left or right table
INNER JOIN
The INNER JOIN
keyword selects records that have matching values in both tables.
Let's look at an example using our dummy testproducts
table:
 testproduct_id |     product_name    ÂÂ
| category_id
----------------+------------------------+-------------
            ÂÂ
1 | Johns Fruit Cake      |         ÂÂ
3
            ÂÂ
2 | Marys Healthy Mix     |         ÂÂ
9
            ÂÂ
3 | Peters Scary Stuff    |        ÂÂ
10
            ÂÂ
4 | Jims Secret Recipe    |        ÂÂ
11
            ÂÂ
5 | Elisabeths Best Apples |        ÂÂ
12
            ÂÂ
6 | Janes Favorite Cheese |         ÂÂ
4
            ÂÂ
7 | Billys Home Made Pizza |        ÂÂ
13
            ÂÂ
8 | Ellas Special Salmon  |         ÂÂ
8
            ÂÂ
9 | Roberts Rich Spaghetti |         ÂÂ
5
           10 |
Mias Popular Ice       |        ÂÂ
14
(10 rows)
We will try to join the testproducts
table
with the categories
table:
 category_id | category_name |                     ÂÂ
description
-------------+----------------+------------------------------------------------------------
         ÂÂ
1 | Beverages     | Soft drinks, coffees, teas,
beers, and ales
         ÂÂ
2 | Condiments    | Sweet and savory sauces, relishes,
spreads, and seasonings
         ÂÂ
3 | Confections   | Desserts, candies, and sweet breads
         ÂÂ
4 | Dairy Products | Cheeses
         ÂÂ
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
         ÂÂ
6 | Meat/Poultry  | Prepared meats
         ÂÂ
7 | Produce       | Dried fruit and bean
curd
          8 |
Seafood       | Seaweed and fish
(8
rows)
Notice that many of the products in testproducts
have a
category_id
that does not match any of the
categories in the categories
table.
By using INNER JOIN
we will not get the
records where there is not a match, we will only get the records that matches
both tables:
Example
Join testproducts
to
categories
using the category_id
column:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
INNER JOIN categories ON
testproducts.category_id = categories.category_id;
Result
Only the records with a match in BOTH tables are returned:
 testproduct_id |     product_name    ÂÂ
| category_name
----------------+------------------------+----------------
            ÂÂ
1 | Johns Fruit Cake      | Confections
            ÂÂ
6 | Janes Favorite Cheese | Dairy Products
            ÂÂ
8 | Ellas Special Salmon  | Seafood
            ÂÂ
9 | Roberts Rich Spaghetti | Grains/Cereals
(4 rows)
Note: JOIN
and INNER JOIN
will give the same result.
INNER
is the default join type for
JOIN
, so when you write
JOIN
the parser actually writes
INNER JOIN
.
LEFT JOIN
The LEFT JOIN
keyword selects ALL records from the "left" table,
and the matching records from the "right" table.
The result is 0 records from the right side if there is no match.
Let's look at an example using our dummy testproducts
table:
 testproduct_id |     product_name    ÂÂ
| category_id
----------------+------------------------+-------------
            ÂÂ
1 | Johns Fruit Cake      |         ÂÂ
3
            ÂÂ
2 | Marys Healthy Mix     |         ÂÂ
9
            ÂÂ
3 | Peters Scary Stuff    |        ÂÂ
10
            ÂÂ
4 | Jims Secret Recipe    |        ÂÂ
11
            ÂÂ
5 | Elisabeths Best Apples |        ÂÂ
12
            ÂÂ
6 | Janes Favorite Cheese |         ÂÂ
4
            ÂÂ
7 | Billys Home Made Pizza |        ÂÂ
13
            ÂÂ
8 | Ellas Special Salmon  |         ÂÂ
8
            ÂÂ
9 | Roberts Rich Spaghetti |         ÂÂ
5
           10 |
Mias Popular Ice       |        ÂÂ
14
(10 rows)
We will try to join the testproducts
table
with the categories
table:
 category_id | category_name |                     ÂÂ
description
-------------+----------------+------------------------------------------------------------
         ÂÂ
1 | Beverages     | Soft drinks, coffees, teas,
beers, and ales
         ÂÂ
2 | Condiments    | Sweet and savory sauces, relishes,
spreads, and seasonings
         ÂÂ
3 | Confections   | Desserts, candies, and sweet breads
         ÂÂ
4 | Dairy Products | Cheeses
         ÂÂ
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
         ÂÂ
6 | Meat/Poultry  | Prepared meats
         ÂÂ
7 | Produce       | Dried fruit and bean
curd
          8 |
Seafood       | Seaweed and fish
(8
rows)
Note: Many of the products in testproducts
have a
category_id
that does not match any of the
categories in the categories
table.
By using LEFT JOIN
we will get all
records from testpoducts
, even the ones with no
match in the categories
table:
Example
Join testproducts
to
categories
using the category_id
column:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
LEFT JOIN categories ON testproducts.category_id = categories.category_id;
Result
All records from testproducts
, and only the matched records from
categories
:
 testproduct_id |     product_name    ÂÂ
| category_name
----------------+------------------------+----------------
            ÂÂ
1 | Johns Fruit Cake      | Confections
            ÂÂ
2 | Marys Healthy Mix     |
            ÂÂ
3 | Peters Scary Stuff    |
            ÂÂ
4 | Jims Secret Recipe    |
            ÂÂ
5 | Elisabeths Best Apples |
            ÂÂ
6 | Janes Favorite Cheese | Dairy Products
            ÂÂ
7 | Billys Home Made Pizza |
            ÂÂ
8 | Ellas Special Salmon  | Seafood
            ÂÂ
9 | Roberts Rich Spaghetti | Grains/Cereals
           ÂÂ
10 | Mias Popular Ice      |
(10 rows)
Note: LEFT JOIN
and
LEFT OUTER JOIN
will give the same result.
OUTER
is the default join type for
LEFT JOIN
, so when you write
LEFT JOIN
the parser actually writes
LEFT OUTER JOIN
.
RIGHT JOIN
The RIGHT JOIN
keyword selects ALL records from the "right" table,
and the matching records from the "left" table.
The result is 0 records from the left side if there is no match.
Let's look at an example using our dummy testproducts
table:
 testproduct_id |     product_name    ÂÂ
| category_id
----------------+------------------------+-------------
            ÂÂ
1 | Johns Fruit Cake      |         ÂÂ
3
            ÂÂ
2 | Marys Healthy Mix     |         ÂÂ
9
            ÂÂ
3 | Peters Scary Stuff    |        ÂÂ
10
            ÂÂ
4 | Jims Secret Recipe    |        ÂÂ
11
            ÂÂ
5 | Elisabeths Best Apples |        ÂÂ
12
            ÂÂ
6 | Janes Favorite Cheese |         ÂÂ
4
            ÂÂ
7 | Billys Home Made Pizza |        ÂÂ
13
            ÂÂ
8 | Ellas Special Salmon  |         ÂÂ
8
            ÂÂ
9 | Roberts Rich Spaghetti |         ÂÂ
5
           10 | Mias Popular Ice      |        ÂÂ
14
(10 rows)
We will try to join the testproducts
table
with the categories
table:
 category_id | category_name |                     ÂÂ
description
-------------+----------------+------------------------------------------------------------
         ÂÂ
1 | Beverages     | Soft drinks, coffees, teas,
beers, and ales
         ÂÂ
2 | Condiments    | Sweet and savory sauces, relishes,
spreads, and seasonings
         ÂÂ
3 | Confections   | Desserts, candies, and sweet breads
         ÂÂ
4 | Dairy Products | Cheeses
         ÂÂ
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
         ÂÂ
6 | Meat/Poultry  | Prepared meats
         ÂÂ
7 | Produce       | Dried fruit and bean
curd
          8 |
Seafood       | Seaweed and fish
(8
rows)
Note: Many of the products in testproducts
have a
category_id
that does not match any of the
categories in the categories
table.
By using RIGHT JOIN
we will get all
records from categories
, even the ones with no
match in the testproducts
table:
Example
Join testproducts
to
categories
using the category_id
column:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
RIGHT JOIN categories ON testproducts.category_id = categories.category_id;
Result
All records from categories
, and only the matched records from
testproducts
:
 testproduct_id |     product_name    ÂÂ
| category_name
----------------+------------------------+----------------
            ÂÂ
1 | Johns Fruit Cake      | Confections
            ÂÂ
6 | Janes Favorite Cheese | Dairy Products
            ÂÂ
8 | Ellas Special Salmon  | Seafood
            ÂÂ
9 | Roberts Rich Spaghetti | Grains/Cereals
              ÂÂ
|                      ÂÂ
| Condiments
              ÂÂ
|                      ÂÂ
| Meat/Poultry
              ÂÂ
|                      ÂÂ
| Beverages
              ÂÂ
|                      ÂÂ
| Produce
(8 rows)
Note: RIGHT JOIN
and
RIGHT OUTER JOIN
will give the same result.
OUTER
is the default join type for
RIGHT JOIN
, so when you write
RIGHT JOIN
the parser actually writes
RIGHT OUTER JOIN
.
FULL JOIN
The FULL JOIN
keyword selects ALL records from
both tables, even if there is not a match. For rows with a match the values from
both tables are available, if there is not a match the empty fields will get the
value NULL
.
Let's look at an example using our dummy testproducts
table:
 testproduct_id |     product_name    ÂÂ
| category_id
----------------+------------------------+-------------
            ÂÂ
1 | Johns Fruit Cake      |         ÂÂ
3
            ÂÂ
2 | Marys Healthy Mix     |         ÂÂ
9
            ÂÂ
3 | Peters Scary Stuff    |        ÂÂ
10
            ÂÂ
4 | Jims Secret Recipe    |        ÂÂ
11
            ÂÂ
5 | Elisabeths Best Apples |        ÂÂ
12
            ÂÂ
6 | Janes Favorite Cheese |         ÂÂ
4
            ÂÂ
7 | Billys Home Made Pizza |        ÂÂ
13
            ÂÂ
8 | Ellas Special Salmon  |         ÂÂ
8
            ÂÂ
9 | Roberts Rich Spaghetti |         ÂÂ
5
           10 | Mias Popular Ice      |        ÂÂ
14
(10 rows)
We will try to join the testproducts
table
with the categories
table:
 category_id | category_name |                     ÂÂ
description
-------------+----------------+------------------------------------------------------------
         ÂÂ
1 | Beverages     | Soft drinks, coffees, teas,
beers, and ales
         ÂÂ
2 | Condiments    | Sweet and savory sauces, relishes,
spreads, and seasonings
         ÂÂ
3 | Confections   | Desserts, candies, and sweet breads
         ÂÂ
4 | Dairy Products | Cheeses
         ÂÂ
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
         ÂÂ
6 | Meat/Poultry  | Prepared meats
         ÂÂ
7 | Produce       | Dried fruit and bean
curd
          8 |
Seafood       | Seaweed and fish
(8
rows)
Note: Many of the products in testproducts
have a
category_id
that does not match any of the
categories in the categories
table.
By using FULL JOIN
we will get all
records from both the categories
table and the
testproducts
table:
Example
Join testproducts
to
categories
using the category_id
column:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
FULL JOIN categories ON testproducts.category_id = categories.category_id;
Result
All records from both tables are returned.
Rows with no match will get a NULL
value
in fields from the opposite table:
 testproduct_id |     product_name     ÂÂ
| category_name
----------------+-------------------------+----------------
            ÂÂ
1 | Johns Fruit Cake       | Confections
            ÂÂ
2 | Marys Healthy Mix      |
            ÂÂ
3 | Peters Scary Stuff     |
            ÂÂ
4 | Jims Secret Recipe     |
            ÂÂ
5 | Elisabeths Best Apples |
            ÂÂ
6 | Janes Favorite Cheese  | Dairy Products
            ÂÂ
7 | Billys Home Made Pizza |
            ÂÂ
8 | Ellas Special Salmon   | Seafood
            ÂÂ
9 | Roberts Rich Spaghetti | Grains/Cereals
           ÂÂ
10 | Mias Popular Ice       |
              ÂÂ
|                       ÂÂ
| Condiments
              ÂÂ
|                       ÂÂ
| Meat/Poultry
              ÂÂ
|                       ÂÂ
| Beverages
              ÂÂ
|                       ÂÂ
| Produce
(14 rows)
Note: FULL JOIN
and
FULL OUTER JOIN
will give the same result.
OUTER
is the default join type for
FULL JOIN
, so when you write
FULL JOIN
the parser actually writes
FULL OUTER JOIN
.
CROSS JOIN
The CROSS JOIN
keyword matches ALL records from
the "left" table with EACH record from the "right" table.
That means that all records from the "right" table will be returned for each
record in the "left" table.
This way of joining can potentially return very large table, and you should
not use it if you do not have to.
Let's look at an example using our dummy testproducts
table:
 testproduct_id |     product_name    ÂÂ
| category_id
----------------+------------------------+-------------
            ÂÂ
1 | Johns Fruit Cake      |         ÂÂ
3
            ÂÂ
2 | Marys Healthy Mix     |         ÂÂ
9
            ÂÂ
3 | Peters Scary Stuff    |        ÂÂ
10
            ÂÂ
4 | Jims Secret Recipe    |        ÂÂ
11
            ÂÂ
5 | Elisabeths Best Apples |        ÂÂ
12
            ÂÂ
6 | Janes Favorite Cheese |         ÂÂ
4
            ÂÂ
7 | Billys Home Made Pizza |        ÂÂ
13
            ÂÂ
8 | Ellas Special Salmon  |         ÂÂ
8
            ÂÂ
9 | Roberts Rich Spaghetti |         ÂÂ
5
           10 | Mias Popular Ice      |        ÂÂ
14
(10 rows)
We will try to join the testproducts
table
with the categories
table:
 category_id | category_name |                     ÂÂ
description
-------------+----------------+------------------------------------------------------------
         ÂÂ
1 | Beverages     | Soft drinks, coffees, teas,
beers, and ales
         ÂÂ
2 | Condiments    | Sweet and savory sauces, relishes,
spreads, and seasonings
         ÂÂ
3 | Confections   | Desserts, candies, and sweet breads
         ÂÂ
4 | Dairy Products | Cheeses
         ÂÂ
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
         ÂÂ
6 | Meat/Poultry  | Prepared meats
         ÂÂ
7 | Produce       | Dried fruit and bean
curd
          8 |
Seafood       | Seaweed and fish
(8
rows)
Note: The CROSS JOIN
method will
return ALL categories
for EACH
testproduct
, meaning that it will return 80
rows (10 * 8).
Example
Join testproducts
to
categories
using the CROSS JOIN
keyword:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
CROSS JOIN categories;
Result
All categories
for each
testproduct
will be returned:
 testproduct_id
|     product_name     |
category_name
----------------+------------------------+----------------
            ÂÂ
1 | Johns Fruit Cake      | Beverages
            ÂÂ
1 | Johns Fruit Cake      | Condiments
            ÂÂ
1 | Johns Fruit Cake      | Confections
            ÂÂ
1 | Johns Fruit Cake      | Dairy Products
            ÂÂ
1 | Johns Fruit Cake      | Grains/Cereals
            ÂÂ
1 | Johns Fruit Cake      | Meat/Poultry
            ÂÂ
1 | Johns Fruit Cake      | Produce
            ÂÂ
1 | Johns Fruit Cake      | Seafood
            ÂÂ
2 | Marys Healthy Mix     | Beverages
            ÂÂ
2 | Marys Healthy Mix     | Condiments
            ÂÂ
2 | Marys Healthy Mix     | Confections
            ÂÂ
2 | Marys Healthy Mix     | Dairy Products
            ÂÂ
2 | Marys Healthy Mix     | Grains/Cereals
            ÂÂ
2 | Marys Healthy Mix     | Meat/Poultry
            ÂÂ
2 | Marys Healthy Mix     | Produce
            ÂÂ
2 | Marys Healthy Mix     | Seafood
            ÂÂ
3 | Peters Scary Stuff    | Beverages
            ÂÂ
3 | Peters Scary Stuff    | Condiments
            ÂÂ
3 | Peters Scary Stuff    | Confections
            ÂÂ
3 | Peters Scary Stuff    | Dairy Products
            ÂÂ
3 | Peters Scary Stuff    | Grains/Cereals
            ÂÂ
3 | Peters Scary Stuff    | Meat/Poultry
            ÂÂ
3 | Peters Scary Stuff    | Produce
            ÂÂ
3 | Peters Scary Stuff    | Seafood
            ÂÂ
4 | Jims Secret Recipe    | Beverages
            ÂÂ
4 | Jims Secret Recipe    | Condiments
            ÂÂ
4 | Jims Secret Recipe    | Confections
            ÂÂ
4 | Jims Secret Recipe    | Dairy Products
            ÂÂ
4 | Jims Secret Recipe    | Grains/Cereals
            ÂÂ
4 | Jims Secret Recipe    | Meat/Poultry
            ÂÂ
4 | Jims Secret Recipe    | Produce
            ÂÂ
4 | Jims Secret Recipe    | Seafood
            ÂÂ
5 | Elisabeths Best Apples | Beverages
            ÂÂ
5 | Elisabeths Best Apples | Condiments
            ÂÂ
5 | Elisabeths Best Apples | Confections
            ÂÂ
5 | Elisabeths Best Apples | Dairy Products
            ÂÂ
5 | Elisabeths Best Apples | Grains/Cereals
            ÂÂ
5 | Elisabeths Best Apples | Meat/Poultry
            ÂÂ
5 | Elisabeths Best Apples | Produce
            ÂÂ
5 | Elisabeths Best Apples | Seafood
            ÂÂ
6 | Janes Favorite Cheese | Beverages
            ÂÂ
6 | Janes Favorite Cheese | Condiments
            ÂÂ
6 | Janes Favorite Cheese | Confections
            ÂÂ
6 | Janes Favorite Cheese | Dairy Products
            ÂÂ
6 | Janes Favorite Cheese | Grains/Cereals
            ÂÂ
6 | Janes Favorite Cheese | Meat/Poultry
            ÂÂ
6 | Janes Favorite Cheese | Produce
            ÂÂ
6 | Janes Favorite Cheese | Seafood
            ÂÂ
7 | Billys Home Made Pizza | Beverages
            ÂÂ
7 | Billys Home Made Pizza | Condiments
            ÂÂ
7 | Billys Home Made Pizza | Confections
            ÂÂ
7 | Billys Home Made Pizza | Dairy Products
            ÂÂ
7 | Billys Home Made Pizza | Grains/Cereals
            ÂÂ
7 | Billys Home Made Pizza | Meat/Poultry
            ÂÂ
7 | Billys Home Made Pizza | Produce
            ÂÂ
7 | Billys Home Made Pizza | Seafood
            ÂÂ
8 | Ellas Special Salmon  | Beverages
            ÂÂ
8 | Ellas Special Salmon  | Condiments
            ÂÂ
8 | Ellas Special Salmon  | Confections
            ÂÂ
8 | Ellas Special Salmon  | Dairy Products
            ÂÂ
8 | Ellas Special Salmon  | Grains/Cereals
            ÂÂ
8 | Ellas Special Salmon  | Meat/Poultry
            ÂÂ
8 | Ellas Special Salmon  | Produce
            ÂÂ
8 | Ellas Special Salmon  | Seafood
            ÂÂ
9 | Roberts Rich Spaghetti | Beverages
            ÂÂ
9 | Roberts Rich Spaghetti | Condiments
            ÂÂ
9 | Roberts Rich Spaghetti | Confections
            ÂÂ
9 | Roberts Rich Spaghetti | Dairy Products
            ÂÂ
9 | Roberts Rich Spaghetti | Grains/Cereals
            ÂÂ
9 | Roberts Rich Spaghetti | Meat/Poultry
            ÂÂ
9 | Roberts Rich Spaghetti | Produce
            ÂÂ
9 | Roberts Rich Spaghetti | Seafood
           ÂÂ
10 | Mias Popular Ice      | Beverages
           ÂÂ
10 | Mias Popular Ice      | Condiments
           ÂÂ
10 | Mias Popular Ice      | Confections
           ÂÂ
10 | Mias Popular Ice      | Dairy Products
           ÂÂ
10 | Mias Popular Ice      | Grains/Cereals
           ÂÂ
10 | Mias Popular Ice      | Meat/Poultry
           ÂÂ
10 | Mias Popular Ice      | Produce
           ÂÂ
10 | Mias Popular Ice      | Seafood
(80
rows)
UNION
The UNION
operator is used to combine the result-set of two or more queries.
The queries in the union must follow these rules:
- They must have the same number of columns
- The columns must have the same data types
- The columns must be in the same order
Example
Combine products
and
testproducts
using the UNION
operator:
SELECT
product_id, product_name
FROM products
UNION
SELECT
testproduct_id, product_name
FROM testproducts
ORDER BY product_id;
UNION vs UNION ALL
With the UNION
operator,
if some rows in the two queries returns the exact same result, only one row will be listed,
because UNION
selects only distinct values.
Use UNION ALL
to return duplicate values.
Let's make some changes to the queries, so that we have duplicate values in the
result:
Example - UNION
SELECT
product_id
FROM products
UNION
SELECT
testproduct_id
FROM testproducts
ORDER BY product_id;
Example - UNION ALL
SELECT
product_id
FROM products
UNION ALL
SELECT
testproduct_id
FROM testproducts
ORDER BY product_id;
GROUP BY
The GROUP BY
clause groups rows that have the same values into summary rows,
like "find the number of customers in each country".
The GROUP BY
clause is often used with aggregate functions like
COUNT()
,
MAX()
,
MIN()
,
SUM()
,
AVG()
to group the result-set by one or more columns.
Example
Lists the number of customers in each country:
SELECT COUNT(customer_id), country
FROM customers
GROUP BY country;
GROUP BY With JOIN
The following SQL statement lists the number of orders made by each customer:
Example
SELECT customers.customer_name, COUNT(orders.order_id)
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customer_name;
HAVING
The HAVING
clause was added to SQL because the
WHERE
clause cannot be used with aggregate functions.
Aggregate functions are often used with GROUP BY
clauses,
and by adding HAVING
we can write condition like we do with
WHERE
clauses.
Example
List only countries that are represented more than 5 times:
SELECT COUNT(customer_id), country
FROM customers
GROUP BY country
HAVING COUNT(customer_id) > 5;
More HAVING Examples
The following SQL statement lists only orders with a total price of 400$ or more:
Example
SELECT order_details.order_id, SUM(products.price)
FROM order_details
LEFT JOIN products ON order_details.product_id = products.product_id
GROUP BY order_id
HAVING SUM(products.price) > 400.00;
Lists customers that have ordered for 1000$ or more:
Example
SELECT customers.customer_name, SUM(products.price)
FROM order_details
LEFT JOIN products ON order_details.product_id = products.product_id
LEFT JOIN orders ON order_details.order_id = orders.order_id
LEFT JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customer_name
HAVING SUM(products.price) > 1000.00;
EXISTS
The EXISTS
operator is used to test for the existence of any record in a
sub query.
The EXISTS
operator returns TRUE if the sub
query returns one or more records.
Example
Return all customers that is represented in the orders
table:
SELECT customers.customer_name
FROM customers
WHERE EXISTS (
ÂÂ
SELECT order_id
ÂÂ
FROM orders
 WHERE customer_id = customers.customer_id
);
The result in example above showed that 89 customers had at least one order in the
orders
table.
NOT EXISTS
To check which customers that do not have any orders, we can use the
NOT
operator together with the EXISTS
operator :
Example
Return all customers that is NOT represented in the orders
table:
SELECT customers.customer_name
FROM customers
WHERE NOT EXISTS (
ÂÂ
SELECT order_id
ÂÂ
FROM orders
 WHERE customer_id = customers.customer_id
);
ANY
The ANY
operator
allows you to perform a comparison between a single column value and a range of other values.
The ANY
operator:
- returns a Boolean value as a result
- returns TRUE if ANY of the sub query values meet the condition
ANY
means that the condition will be true if the operation is true for any of the values in the range.
Example
List products that have ANY records in the order_details
table with a quantity
larger than 120:
SELECT product_name
FROM products
WHERE product_id = ANY (
 SELECT
product_id
 FROM order_details
 WHERE quantity > 120
);
ALL
The ALL
operator:
- returns a Boolean value as a result
- returns TRUE if ALL of the sub query values meet the condition
- is used with
SELECT
, WHERE
and HAVING
statements
ALL
means that the condition will be true only if the operation is true for all values in the range.
Example
List the products if ALL the records in the order_details with quantity larger than 10.
Note: This will of course return FALSE because the quantity column has many different values (not only the value of 10):
SELECT product_name
FROM products
WHERE product_id = ALL (
 SELECT
product_id
 FROM order_details
 WHERE quantity > 10
);
CASE
The CASE
expression goes through conditions and returns a value when the first condition is met
(like an if-then-else statement).
Once a condition is true, it will stop reading and return the result.
If no conditions are true, it returns the value in the ELSE
clause.
If there is no ELSE
part and no conditions are true, it returns NULL.
Example
Return specific values if the price meets a specific condition:
SELECT product_name,
CASE
ÂÂ
WHEN price < 10 THEN 'Low price product'
 WHEN price > 50 THEN 'High
price product'
ELSE
 'Normal product'
END
FROM
products;
With an Alias
When a column name is not specified for the "case" field,
the parser uses case
as the column name.
To specify a column name, add an alias after the END
keyword.
Example
Same example, but with an alias for the case column::
SELECT product_name,
CASE
ÂÂ
WHEN price < 10 THEN 'Low price product'
 WHEN price > 50 THEN 'High
price product'
ELSE
 'Normal product'
END AS "price category"
FROM
products;
You can read more about aliases in our .
PostgreSQL Exercises
You can test your PostgreSQL skills with W3Schools' Exercises.
Exercises
We have gathered a variety of PostgreSQL exercises (with answers) for each PostgreSQL Chapter.
Try to solve an exercise by editing some code, or show the answer to see what you've done wrong.
Count Your Score
You will get 1 point for
each correct answer. Your score and total score will always be displayed.
Start PostgreSQL Exercises
Good luck!
If you don't know PostgreSQL, we suggest that you read our from scratch.
PostgreSQL Quiz
You can test your PostgreSQL skills with W3Schools' Quiz.
The Test
The test contains 25 questions and there is no time limit.
The test is not official, it's just a nice way to see how much you know about
PostgreSQL.
Count Your Score
You will get 1 point for each correct answer. At the end of the Quiz,
your total score will be displayed. Maximum score is 25 points.
Start the Quiz
Good luck!
If you do not know PostgreSQL, we suggest that you read our from scratch.
Login