├── .gitignore ├── 1-queries ├── 01_introduction.sql ├── 02_select.sql ├── 05_like.sql ├── 03_streaming_wars.sql ├── 04_rotten_tomatoes.sql ├── 06_golden_age.sql ├── 07_order_by.sql └── 08_nyc_restaurants.sql ├── 2-aggregates ├── 09_music_playlist.sql ├── 10_counting_rows.sql ├── 12_total_playtime.sql ├── 13_billboard_hot_100.sql ├── 14_group_by.sql ├── 11_old_new.sql └── 15_video_games.sql ├── 4-multiple-tables ├── 27_online_shop.sql ├── 26_students_teachers.sql ├── 23_locked_in.sql ├── 25_left_join.sql ├── 24_inner_join.sql └── 22_joins.sql ├── 3-new-table ├── 18_alter_table.sql ├── 20_delete_from.sql ├── 16_create_table.sql ├── 19_update_set.sql ├── 17_insert_into.sql └── 21_bffs.sql ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /1-queries/01_introduction.sql: -------------------------------------------------------------------------------- 1 | -- Introduction 🛢️ 2 | -- Codédex 3 | 4 | SELECT * FROM shows; 5 | -------------------------------------------------------------------------------- /1-queries/02_select.sql: -------------------------------------------------------------------------------- 1 | -- SELECT 📺 2 | -- Codédex 3 | 4 | SELECT name, genre 5 | FROM shows; 6 | -------------------------------------------------------------------------------- /2-aggregates/09_music_playlist.sql: -------------------------------------------------------------------------------- 1 | -- Music Playlist 💿 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM playlist; -------------------------------------------------------------------------------- /1-queries/05_like.sql: -------------------------------------------------------------------------------- 1 | -- Like 😂 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM shows 6 | WHERE genre LIKE '%com%'; 7 | -------------------------------------------------------------------------------- /2-aggregates/10_counting_rows.sql: -------------------------------------------------------------------------------- 1 | -- Counting Rows 🐦‍⬛ 2 | -- Codédex 3 | 4 | SELECT COUNT(*) 5 | FROM playlist; 6 | -------------------------------------------------------------------------------- /2-aggregates/12_total_playtime.sql: -------------------------------------------------------------------------------- 1 | -- Total Playtime ⏱️ 2 | -- Codédex 3 | 4 | SELECT SUM(duration) 5 | FROM playlist; -------------------------------------------------------------------------------- /4-multiple-tables/27_online_shop.sql: -------------------------------------------------------------------------------- 1 | -- Online Shop 🛍️ 2 | -- Codédex 3 | 4 | -- No solution for this one yet! :) -------------------------------------------------------------------------------- /1-queries/03_streaming_wars.sql: -------------------------------------------------------------------------------- 1 | -- Streaming Wars 🤺 2 | -- Codédex 3 | 4 | SELECT DISTINCT stream 5 | FROM shows; 6 | -------------------------------------------------------------------------------- /2-aggregates/13_billboard_hot_100.sql: -------------------------------------------------------------------------------- 1 | -- Billboard Hot 100 ⏰ 2 | -- Codédex 3 | 4 | SELECT AVG(duration) 5 | FROM playlist; -------------------------------------------------------------------------------- /3-new-table/18_alter_table.sql: -------------------------------------------------------------------------------- 1 | -- Alter Table 🕸️ 2 | -- Codédex 3 | 4 | ALTER TABLE companies 5 | ADD COLUMN website TEXT; -------------------------------------------------------------------------------- /3-new-table/20_delete_from.sql: -------------------------------------------------------------------------------- 1 | -- Delete From ➖ 2 | -- Codédex 3 | 4 | DELETE FROM companies 5 | WHERE name = 'BeReal'; -------------------------------------------------------------------------------- /2-aggregates/14_group_by.sql: -------------------------------------------------------------------------------- 1 | -- GROUP BY 👯 2 | -- Codédex 3 | 4 | SELECT artist, avg(plays) 5 | FROM playlist 6 | GROUP BY artist; -------------------------------------------------------------------------------- /1-queries/04_rotten_tomatoes.sql: -------------------------------------------------------------------------------- 1 | -- Rotten Tomatoes 🤢 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM shows 6 | WHERE tomatometer < 60; 7 | -------------------------------------------------------------------------------- /1-queries/06_golden_age.sql: -------------------------------------------------------------------------------- 1 | -- Golden Age 🌟 2 | -- Codédex 3 | 4 | SELECT id, name, year 5 | FROM shows 6 | WHERE year BETWEEN 1999 AND 2024; 7 | -------------------------------------------------------------------------------- /1-queries/07_order_by.sql: -------------------------------------------------------------------------------- 1 | -- ORDER BY 🔝 2 | -- Codédex 3 | 4 | SELECT name, genre, stream, tomatometer 5 | FROM shows 6 | ORDER BY tomatometer DESC; -------------------------------------------------------------------------------- /4-multiple-tables/26_students_teachers.sql: -------------------------------------------------------------------------------- 1 | -- Students & Teachers 🏫 2 | -- Codédex 3 | 4 | SELECT name 5 | FROM students 6 | UNION 7 | SELECT name 8 | FROM teachers; -------------------------------------------------------------------------------- /4-multiple-tables/23_locked_in.sql: -------------------------------------------------------------------------------- 1 | -- Locked In 🔐 2 | -- Codédex 3 | 4 | -- books table's author_id column (foreign key) 🤝 authors table's id column (primary key) 5 | -------------------------------------------------------------------------------- /2-aggregates/11_old_new.sql: -------------------------------------------------------------------------------- 1 | -- Old & New ⏳ 2 | -- Codédex 3 | 4 | SELECT title, artist, MIN(year) 5 | FROM playlist; 6 | 7 | SELECT title, artist, MAX(year) 8 | FROM playlist; -------------------------------------------------------------------------------- /3-new-table/16_create_table.sql: -------------------------------------------------------------------------------- 1 | -- Create Table 🆕 2 | -- Codédex 3 | 4 | CREATE TABLE companies ( 5 | id INTEGER, 6 | name TEXT, 7 | headquarters TEXT, 8 | year INTEGER 9 | ); -------------------------------------------------------------------------------- /2-aggregates/15_video_games.sql: -------------------------------------------------------------------------------- 1 | -- Video Games 🎮 2 | -- Codédex 3 | 4 | SELECT title, year, MAX(players) 5 | FROM games; 6 | 7 | SELECT language, COUNT(*) 8 | FROM games 9 | GROUP BY language; 10 | 11 | SELECT genre, AVG(metascore) 12 | FROM games 13 | GROUP BY genre; -------------------------------------------------------------------------------- /4-multiple-tables/25_left_join.sql: -------------------------------------------------------------------------------- 1 | -- Left Join 🤝 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM books 6 | LIMIT 5; 7 | 8 | SELECT * 9 | FROM authors 10 | LIMIT 5; 11 | 12 | SELECT book_id, title, year 13 | FROM books 14 | LEFT JOIN authors 15 | ON books.author_id = authors.id; -------------------------------------------------------------------------------- /4-multiple-tables/24_inner_join.sql: -------------------------------------------------------------------------------- 1 | -- Inner Join 🤝 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM books 6 | LIMIT 5; 7 | 8 | SELECT * 9 | FROM authors 10 | LIMIT 5; 11 | 12 | SELECT book_id, title, year 13 | FROM books 14 | INNER JOIN authors 15 | ON books.author_id = authors.id; -------------------------------------------------------------------------------- /4-multiple-tables/22_joins.sql: -------------------------------------------------------------------------------- 1 | -- Joins 🤝 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM movies; 6 | 7 | SELECT * 8 | FROM directors; 9 | 10 | SELECT * 11 | FROM books; 12 | 13 | SELECT * 14 | FROM authors; 15 | 16 | SELECT * 17 | FROM students; 18 | 19 | SELECT * 20 | FROM teachers; -------------------------------------------------------------------------------- /3-new-table/19_update_set.sql: -------------------------------------------------------------------------------- 1 | -- Update Set 🔄 2 | -- Codédex 3 | 4 | UPDATE companies 5 | SET website = 'x.com' 6 | WHERE id = 1; 7 | 8 | UPDATE companies 9 | SET website = 'duolingo.com' 10 | WHERE id = 2; 11 | 12 | UPDATE companies 13 | SET website = 'bereal.com' 14 | WHERE id = 3; 15 | 16 | UPDATE companies 17 | SET website = 'codedex.io' 18 | WHERE id = 4; 19 | 20 | SELECT * 21 | FROM companies; -------------------------------------------------------------------------------- /3-new-table/17_insert_into.sql: -------------------------------------------------------------------------------- 1 | -- Insert Into ➕ 2 | -- Codédex 3 | 4 | INSERT INTO companies (id, name, headquarters, year) 5 | VALUES (1, 'Twitter', 'San Francisco 🌁', 2006); 6 | 7 | INSERT INTO companies (id, name, headquarters, year) 8 | VALUES (2, 'Duolingo', 'Pittsburgh 🐝', 2011); 9 | 10 | INSERT INTO companies (id, name, headquarters, year) 11 | VALUES (3, 'BeReal', 'Paris 🇫🇷', 2020); 12 | 13 | INSERT INTO companies (id, name, headquarters, year) 14 | VALUES (4, 'Codedex', 'New York 🗽', 2022); 15 | -------------------------------------------------------------------------------- /1-queries/08_nyc_restaurants.sql: -------------------------------------------------------------------------------- 1 | -- NYC Restaurants 😋 2 | -- Codédex 3 | 4 | SELECT * 5 | FROM restaurants; 6 | 7 | -- Unique restaurants 8 | 9 | SELECT DISTINCT cuisine 10 | FROM restaurants; 11 | 12 | -- Chinese spots 13 | 14 | SELECT * 15 | FROM restaurants 16 | WHERE cuisine = 'Chinese'; 17 | 18 | -- Italian spots 19 | 20 | SELECT * 21 | FROM restaurants 22 | WHERE cuisine = 'Italian'; 23 | 24 | -- Restaurants in Greenpoint 25 | 26 | SELECT * 27 | FROM restaurants 28 | WHERE neighborhood = 'Greenpoint'; 29 | 30 | -- Cheap eats 31 | 32 | SELECT * 33 | FROM restaurants 34 | WHERE price = '$'; 35 | 36 | -- Bougie spots 37 | 38 | SELECT * 39 | FROM restaurants 40 | WHERE price = '$$$'; 41 | -------------------------------------------------------------------------------- /3-new-table/21_bffs.sql: -------------------------------------------------------------------------------- 1 | -- BFFs 👯 2 | -- Codédex 3 | 4 | CREATE TABLE bffs ( 5 | name TEXT, 6 | birthday TEXT, 7 | location TEXT, 8 | note TEXT 9 | ); 10 | 11 | INSERT INTO bffs (name, birthday, location, note) 12 | VALUES ('Ilana', '1987-04-12', 'Gowanus', 'free-spirited NYU grad, owes me $20'); 13 | 14 | INSERT INTO bffs (name, birthday, location, note) 15 | VALUES ('Abbi', '1984-02-01', 'Astoria', 'aspiring illustrator, custodian at Solustice'); 16 | 17 | INSERT INTO bffs (name, birthday, location, note) 18 | VALUES ('Trey', '1982-11-21', 'East Village', 'Soulstice trainer'); 19 | 20 | INSERT INTO bffs (name, birthday, location, note) 21 | VALUES ('Jaimé', '1985-11-26', 'Gowanus', 'Ilana roommate'); 22 | 23 | INSERT INTO bffs (name, birthday, location, note) 24 | VALUES ('Lincoln', '1983-02-04', 'Williamsburg', 'Dentist, easygoing'); 25 | 26 | SELECT * 27 | FROM bffs; 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Codédex 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |

SQL 🛢️

4 | GitHub repo with beginner-friendly problems in SQL 5 |
6 |
7 | 8 | Welcome to SQL GitHub repo! We are super excited to have you. Here, you will find all the solutions to the Codédex exercises. Feel free to make pull requests to add your own twists on the exercises! 9 | 10 | ### Website: www.codedex.io/sql 11 | 12 | 13 | 14 | ## Queries 15 | 16 | - [`introduction.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/01_introduction.sql) 17 | - [`select.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/02_select.sql) 18 | - [`streaming_wars.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/03_streaming_wars.sql) 19 | - [`rotten_tomatoes.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/04_rotten_tomatoes.sql) 20 | - [`like.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/05_like.sql) 21 | - [`golden_age.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/06_golden_age.sql) 22 | - [`order_by.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/07_order_by.sql) 23 | - [`nyc_restaurants.sql`](https://github.com/codedex-io/sql-101/blob/main/1-queries/08_nyc_restaurants.sql) 24 | 25 | 26 | 27 | ## Aggregates 28 | 29 | - [`music_playlist.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/09_music_playlist.sql) 30 | - [`counting_rows.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/10_counting_rows.sql) 31 | - [`old_new.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/11_old_new.sql) 32 | - [`total_playtime.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/12_total_playtime.sql) 33 | - [`billboard_hot_100.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/13_billboard_hot_100.sql) 34 | - [`group_by.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/14_group_by.sql) 35 | - [`video_games.sql`](https://github.com/codedex-io/sql-101/blob/main/2-aggregates/15_video_games.sql) 36 | 37 | ## New Table 38 | 39 | - [`create_table.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/16_create_table.sql) 40 | - [`insert_into.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/17_insert_into.sql) 41 | - [`alter_table.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/18_alter_table.sql) 42 | - [`update_set.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/19_update_set.sql) 43 | - [`delete_from.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/20_delete_from.sql) 44 | - [`bffs.sql`](https://github.com/codedex-io/sql-101/blob/main/3-new-table/21_bffs.sql) 45 | 46 | --- 47 | 48 | Make sure to join the [community](https://www.codedex.io/community) and [Codédex Club](https://www.codedex.io/pricing) for more content! 💖 49 | --------------------------------------------------------------------------------