[SQL ZOO] SELECT within SELECT Tutorial과 The JOIN Operation

크리링·2022년 9월 19일
0

SQL ZOO

목록 보기
1/3
post-thumbnail

SQL을 공부해보자!

SELECT within SELECT Tutorial

  1. List each country name where the population is larger than that of 'Russia'.
world(name, continent, area, population, gdp)

ANSWER :

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')



  1. Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.

ANSWER :

SELECT name FROM world
WHERE continent='Europe' AND gdp/population >
 (SELECT gdp/population FROM world
 WHERE name='United Kingdom')
 



  1. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

ANSWER :

 SELECT name, continent FROM world
WHERE continent IN (
SELECT continent FROM world
WHERE name='Argentina' OR name='Australia'
)
ORDER BY name ASC;



  1. Which country has a population that is more than United Kingom but less than Germany? Show the name and the population.

ANSWER :

SELECT name, population FROM world
WHERE population > (
SELECT population FROM world
WHERE name='Canada'
) AND population< (
SELECT population FROM world
WHERE name='Poland'
)



  1. Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

ANSWER :

SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE name='Germany')*100, 0),'%')
FROM world
WHERE continent='Europe' 



  1. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)

ANSWER :

SELECT name 
FROM world
WHERE gdp > ALL(SELECT gdp 
                 FROM world
                 WHERE continent='Europe'
                 AND gdp IS NOT NULL);



  1. Find the largest country (by area) in each continent, show the continent, the name and the area:

ANSWER :

SELECT continent, name, area
FROM world x
WHERE area >= ALL(SELECT area FROM world y WHERE x.continent=y.continent)



  1. List each continent and the name of the country that comes first alphabetically.

ANSWER :

SELECT continent, name
FROM world x
WHERE name <= ALL(SELECT name FROM world y WHERE x.continent=y.continent)



  1. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.

ANSWER :

SELECT name, continent, population
FROM world x
WHERE 25000000 >= ALL(SELECT population FROM world y WHERE x.continent=y.continent)



  1. Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.

ANSWER :

SELECT name, continent
FROM world x
WHERE x.population > ALL(SELECT population*3 FROM world y WHERE x.continent=y.continent AND x.name != y.name)




The JOIN Operation

  1. The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime

Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'

Answer :

SELECT matchid, player
FROM goal 
  WHERE teamid LIKE '%GER'



  1. From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.

Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.

Show id, stadium, team1, team2 for just game 1012

Answer :

SELECT id,stadium,team1,team2
FROM game
WHERE id LIKE '%1012%'



  1. You can combine the two steps into a single query with a JOIN.
SELECT *
  FROM game JOIN goal ON (id=matchid)

The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game. (If we wanted to be more clear/specific we could say
ON (game.id=goal.matchid)

The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.

Modify it to show the player, teamid, stadium and mdate for every German goal.

Answer :

SELECT player, teamid, stadium, mdate
FROM game
JOIN goal ON (id=matchid)
WHERE teamid LIKE '%Ger%'



  1. Use the same JOIN as in the previous question.

Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

Answer :

SELECT team1, team2, player
FROM game
JOIN goal ON (id=matchid)
WHERE player LIKE 'Mario%'




5. The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id

Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

SELECT player, teamid, coach, gtime
FROM goal 
JOIN eteam ON (teamid=id)
WHERE gtime<=10




6. To JOIN game with eteam you could use either
game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON (team2=eteam.id)

Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id

List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.

SELECT mdate, teamname
FROM game
JOIN eteam ON (game.team1=eteam.id)
WHERE coach LIKE 'Fernando Santos'




7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'

SELECT player
FROM goal
JOIN game ON (matchid = id)
WHERE stadium LIKE 'National Stadium, Warsaw'




8. The example query shows all goals scored in the Germany-Greece quarterfinal.
Instead show the name of all players who scored a goal against Germany.

SELECT DISTINCT(player)
FROM goal
JOIN game ON (matchid = id)
WHERE (team1 LIKE 'GER' OR team2 LIKE 'GER') AND NOT teamid LIKE 'GER'




9. Show teamname and the total number of goals scored.

SELECT teamname, COUNT(teamname)
FROM goal
JOIN eteam ON (teamid=id)
GROUP BY teamname




10. Show the stadium and the number of goals scored in each stadium.

SELECT stadium, COUNT(stadium)
FROM game
JOIN goal ON (id=matchid)
GROUP BY stadium




11. For every match involving 'POL', show the matchid, date and the number of goals scored.

SELECT matchid, mdate, COUNT(matchid)
FROM game
JOIN goal ON (id=matchid)
WHERE team1 LIKE 'POL' OR team2 LIKE 'POL'
GROUP BY matchid, mdate




12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'

SELECT matchid, mdate, COUNT(matchid)
FROM game
JOIN goal ON (id=matchid)
WHERE teamid LIKE 'GER'
GROUP BY matchid, mdate

1개의 댓글

comment-user-thumbnail
2024년 9월 15일

Sophia had always been fascinated by the fantasy genre, so when she discovered an online slot game with a magical theme, she was immediately intrigued. The game promised an immersive experience with enchanting graphics https://micasino-cl.cl/ and a captivating storyline that drew her into a fantastical world. Sophia decided to dive in, starting with small bets as she familiarized herself with the game’s features and bonus rounds. The slot’s whimsical animations and magical symbols created an engaging and entertaining atmosphere.

답글 달기
Powered by GraphCDN, the GraphQL CDN