{"id":25164,"date":"2022-11-23T05:03:58","date_gmt":"2022-11-23T13:03:58","guid":{"rendered":"https:\/\/coderpad.io\/?p=25164"},"modified":"2023-08-04T13:22:31","modified_gmt":"2023-08-04T20:22:31","slug":"tiny-interviews-world-cup-sql","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/coding-challenges\/tiny-interviews-world-cup-sql\/","title":{"rendered":"Code Challenge: World Cup SQL"},"content":{"rendered":"\n<p>The FIFA World Cup is a global sporting event where national soccer teams compete to determine the world champion every four years! Use your SQL knowledge to discover exciting world cup history facts like \u201cwhich team has the largest world cup following in FIFA history?\u201d Use the sandbox below to get started and practice your SQL skills!&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 Warm up your SQL skills with this article on <a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">SQL functions and techniques every data person should know<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p>Please see the interview prompts written directly in the pad. <strong>Question solutions are provided in the next section after the sandbox<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u203c\ufe0f Attempt the questions in the pad before checking out the solutions.<\/p>\n<\/blockquote>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=235316&#038;use_question_button\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Useful tips<\/h2>\n\n\n\n<p>Running SQL: Comment out any SQL code from a previous question before moving to the next Question (or else all your SQL code will run at once). You can do this quickly by:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Highlighting your code<\/li>\n\n\n\n<li>Typing <code>Cmd + \/<\/code> or <code>Ctrl + \/<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Viewing data<\/h3>\n\n\n\n<p>To view all data in a single row easily, replace the semicolon at the end of your query with <code>\/G<\/code>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>&#8212; Returns a single row in a vertical, easy to ready format<\/p>\n\n\n\n<p><code>select * from World_Cup_Matches limit 1 \\G<\/code><\/p>\n\n\n\n<p>&#8212; Returns a single row in a horizontal format, better suited for a wide screen<\/p>\n\n\n\n<p><code>select * from World_Cup_Matches limit 1;<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interview data<\/h2>\n\n\n\n<p>The data for the tiny interview can be viewed on this <a href=\"https:\/\/www.kaggle.com\/datasets\/abecklas\/fifa-world-cup?select=WorldCupMatches.csv\" target=\"_blank\" rel=\"noopener\">kaggle dataset<\/a>.<\/p>\n\n\n\n<p>CoderPad provides a basic SQL sandbox with the following schema. You can also use commands like <code>show tables<\/code> and <code>desc world_cup_tournaments<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637e1a3e3b5a9.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The schema diagram for the <code>world_cup_tournaments<\/code>, <code>world_cup_matches<\/code> and <code>world_cup_line_ups<\/code> table.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Solutions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Question 1<\/h3>\n\n\n\n<p><em>Learn the skills needed:<\/em><\/p>\n\n\n\n<p>ORDER BY: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_orderby.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_orderby.asp<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">select<\/span> \n<span class=\"hljs-keyword\">Year<\/span>\n, Host_Country\n, Goals_Scored\n<span class=\"hljs-keyword\">from<\/span> world_cup_tournaments\n<span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> Goals_Scored <span class=\"hljs-keyword\">desc<\/span> <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Question 2<\/h3>\n\n\n\n<p><em>Learn the skills needed:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>LEFT JOIN: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_join_left.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_join_left.asp<\/a><\/li>\n\n\n\n<li>COUNT(): <a href=\"https:\/\/www.w3schools.com\/sql\/func_mysql_count.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/func_mysql_count.asp<\/a><\/li>\n\n\n\n<li>GROUP BY: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_groupby.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_groupby.asp<\/a><\/li>\n\n\n\n<li>ORDER BY: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_orderby.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_orderby.asp<\/a><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">select<\/span> \n  a.Year\n  , a.Host_Country\n  , <span class=\"hljs-keyword\">count<\/span>(<span class=\"hljs-keyword\">distinct<\/span> b.Stadium) <span class=\"hljs-keyword\">as<\/span> num_pitch_locations \n<span class=\"hljs-keyword\">from<\/span> \n  world_cup_tournaments <span class=\"hljs-keyword\">as<\/span> a \n  <span class=\"hljs-keyword\">left<\/span> <span class=\"hljs-keyword\">join<\/span> world_cup_matches <span class=\"hljs-keyword\">as<\/span> b <span class=\"hljs-keyword\">on<\/span> a.Year = b.Year \n<span class=\"hljs-keyword\">group<\/span> <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">desc<\/span> <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Question 3<\/h3>\n\n\n\n<p><em>Learn the skills needed:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Common Table Expressions (CTE): <a href=\"https:\/\/www.w3schools.blog\/sql-cte-how-to-master-it-with-easy-examples\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.blog\/sql-cte-how-to-master-it-with-easy-examples<\/a> <a href=\"https:\/\/mariadb.com\/kb\/en\/non-recursive-common-table-expressions-overview\/\" target=\"_blank\" rel=\"noopener\">https:\/\/mariadb.com\/kb\/en\/non-recursive-common-table-expressions-overview\/<\/a><\/li>\n\n\n\n<li>ORDER BY: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_orderby.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_orderby.asp<\/a><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">with<\/span> calculate_avg_goals_per_match <span class=\"hljs-keyword\">as<\/span> (\n  <span class=\"hljs-keyword\">select<\/span> \n    <span class=\"hljs-keyword\">Year<\/span>\n    , Host_Country \n    , Goals_Scored \/ Matches_Played <span class=\"hljs-keyword\">as<\/span> avg_goals_per_match\n  <span class=\"hljs-keyword\">from<\/span> world_cup_tournaments\n)\n<span class=\"hljs-keyword\">select<\/span> *\n<span class=\"hljs-keyword\">from<\/span> calculate_avg_goals_per_match\n<span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> avg_goals_per_match <span class=\"hljs-keyword\">desc<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Question 4<\/h3>\n\n\n\n<p><em>Learn the skills needed:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Common Table Expressions (CTE): <a href=\"https:\/\/www.w3schools.blog\/sql-cte-how-to-master-it-with-easy-examples\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.blog\/sql-cte-how-to-master-it-with-easy-examples<\/a> <a href=\"https:\/\/mariadb.com\/kb\/en\/non-recursive-common-table-expressions-overview\/\" target=\"_blank\" rel=\"noopener\">https:\/\/mariadb.com\/kb\/en\/non-recursive-common-table-expressions-overview\/<\/a><\/li>\n\n\n\n<li>UNION ALL: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_ref_union_all.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_ref_union_all.asp<\/a><\/li>\n\n\n\n<li>ORDER BY: <a href=\"https:\/\/www.w3schools.com\/sql\/sql_orderby.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/sql\/sql_orderby.asp<\/a><\/li>\n\n\n\n<li>SUM(): <a href=\"https:\/\/www.w3schools.com\/mysql\/func_mysql_sum.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/mysql\/func_mysql_sum.asp<\/a><\/li>\n\n\n\n<li>LIMIT: <a href=\"https:\/\/www.w3schools.com\/mysql\/mysql_limit.asp\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3schools.com\/mysql\/mysql_limit.asp<\/a><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">with<\/span> list_matches_with_attendance <span class=\"hljs-keyword\">as<\/span> (\n  <span class=\"hljs-comment\">-- List stadium_attendance for Home_Team_Name <\/span>\n  <span class=\"hljs-keyword\">select<\/span> \n    Match_ID \n    , Home_Team_Name <span class=\"hljs-keyword\">as<\/span> team_name \n    , Attendance <span class=\"hljs-keyword\">as<\/span> stadium_attendance\n  <span class=\"hljs-keyword\">from<\/span> world_cup_matches\n\n  <span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">ALL<\/span> \n\n  <span class=\"hljs-comment\">-- List all stadium_attendance for Away_Team_Name <\/span>\n  <span class=\"hljs-keyword\">select<\/span> \n    Match_ID \n    , Away_Team_Name <span class=\"hljs-keyword\">as<\/span> team_name \n    , Attendance <span class=\"hljs-keyword\">as<\/span> stadium_attendance\n  <span class=\"hljs-keyword\">from<\/span> world_cup_matches\n)\n<span class=\"hljs-keyword\">select<\/span> \n  team_name\n  , <span class=\"hljs-keyword\">sum<\/span>(stadium_attendance) <span class=\"hljs-keyword\">as<\/span> team_following \n<span class=\"hljs-keyword\">from<\/span> list_matches_with_attendance\n<span class=\"hljs-keyword\">group<\/span> <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">desc<\/span> \n<span class=\"hljs-keyword\">limit<\/span> <span class=\"hljs-number\">10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">More interviewing resources<\/h2>\n\n\n\n<p>Even for the most experienced managers, hiring the right engineers can be tricky.&nbsp;<\/p>\n\n\n\n<p>To make it easier, here are some more resources to help make your hiring process as smooth as possible:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/interviewing\/sql-interview-questions\/\">SQL interview questions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/interviewing\/a-holistic-guide-to-diversity-and-inclusion-in-the-technical-hiring-process\/\" target=\"_blank\" rel=\"noreferrer noopener\">A Holistic Guide to Diversity and Inclusion in the Technical Hiring Process<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/interview-questions\/mysql-interview-questions\/\">25+ questions for MySQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/interview-questions\/postgresql-interview-questions\/\">25+ questions for PostgreSQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/online-coding-tests\/mysql-online-coding-test\/\">Online coding test questions for MySQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/online-coding-tests\/postgresql\/\">Online coding test questions for PostgreSQL<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The FIFA World Cup is a global sporting event where national soccer teams compete to determine the world champion every four years! Use your SQL knowledge to discover exciting world cup history facts like \u201cwhich team has the largest world cup following in FIFA history?\u201d Use the sandbox below to get started and practice your SQL skills!\u00a0<\/p>\n","protected":false},"author":1,"featured_media":25181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[22],"tags":[],"persona":[29],"blog-programming-language":[66],"keyword-cluster":[],"class_list":["post-25164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding-challenges"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=25164"}],"version-history":[{"count":64,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25164\/revisions"}],"predecessor-version":[{"id":35867,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25164\/revisions\/35867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/25181"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=25164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=25164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=25164"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=25164"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=25164"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=25164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}