SQLZOO及经典面试50题刷题记录

文章目录

  • SQLZOO
    • SELECT names
      • 1.顯示所有國家名字,其首都和國家名字是相同的。
      • 2.“Mexico 墨西哥”的首都是”Mexico City”。
      • 3.找出所有首都和其國家名字,而首都要有國家名字中出現
      • 4.找出所有首都和其國家名字,而首都是國家名字的延伸。
      • 5.“Monaco-Ville"是合併國家名字 “Monaco” 和延伸詞”-Ville". 顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
    • SELECT from WORLD Tutorial
      • 1.顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
      • 2.round
      • 3.Show the name - but substitute Australasia for Oceania - for countries beginning with N.
      • 4.Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B
    • SELECT from Nobel Tutorial
      • 1.Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
    • Nobel Quiz
      • 1.顯示有多少年沒有頒發醫學獎。
    • 2.選擇代碼以顯示哪一年沒有頒發物理獎,亦沒有頒發化學獎。
    • SELECT within SELECT Tutorial
      • 1.在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)
      • 2.列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
      • 2.找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字name,continent 洲份和population人口。
      • 3.有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。
    • SUM and COUNT
      • 1.對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目。
      • 2.列出有至少100百萬(1億)(100,000,000)人口的洲份。
    • The nobel table can be used to practice more SUM and COUNT functions.
      • 1.對每一個獎項(Subject),列出有多少年曾頒發過。
      • 2.列出哪年曾同年有3個物理獎Physics得獎者。
      • 3.哪年哪獎項,是同一獎項(subject)頒發給3個人。只列出2000年及之後的資料。
    • JOIN
      • 1.List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.
      • 2.Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.
    • More JOIN operations
      • 1.列出1962年首影的電影及它的第1主角。
      • 2.尊·特拉華達'John Travolta'最忙是哪一年? 顯示年份和該年的電影數目。
      • 3.列出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角。
      • 4.列出曾與演員亞特·葛芬柯'Art Garfunkel'合作過的演員姓名。
  • SQL面试50题
      • 1.查询没有学全所有课的学生的学号、姓名
      • ★2.查询各科排名前二的记录
      • ★3.查询各学生的年龄
      • 4.查询所有学生的学号、姓名、选课数、总成绩
      • ★5.查询出每门课程的及格人数和不及格人数
      • ★6.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计:各分数段人数,课程号和课程名称
      • ★7.行列互换
      • 8.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
      • 9.查询学过编号为“0001”的课程并且也学过编号为“0002”的课程的学生的学号、姓名
      • ★10.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
      • ★11.查询学生平均成绩及其名次
      • ★12.按每门课成绩排名
      • ★13.查询每门功成绩最好的前三名学生姓名
      • 13.找出语文课中成绩第二高的学生成绩。如果不存在第二高成绩的学生,那么查询应返回 null。
      • ★14.查询各学生成绩涨幅
      • ★15.行行交换
      • ★16.统计每个班每个同学成绩平均分大于80分的人数和人数占比
      • ★17.计算新增用户、留存及留存率
      • 18.查找前20%的数据
      • ★19.正则表达式
      • ★20.删除重复的行并保留第一次出现的行
      • ★21.行转列
      • ★22.查询消费的金额都大于100的用户

SQLZOO

SELECT names

1.顯示所有國家名字,其首都和國家名字是相同的。

SELECT
	`name` 
FROM
	world 
WHERE
	name = capital

2.“Mexico 墨西哥”的首都是”Mexico City”。

顯示所有國家名字,其首都是國家名字加上”City”。

SELECT
	`name` 
FROM
	world 
WHERE
	capital = concat(
		`name`,
		' City' 
	)

3.找出所有首都和其國家名字,而首都要有國家名字中出現

SELECT
	capital,
	`name` 
FROM
	world 
WHERE
	capital LIKE concat(
		'%',
		`name`,
		'%' 
	)

4.找出所有首都和其國家名字,而首都是國家名字的延伸。

你應顯示 Mexico City,因它比其國家名字 Mexico 長,你不應顯示 Luxembourg,因它的首都和國家名相是相同的。

SELECT
	`name`,
	capital 
FROM
	world 
WHERE
	capital LIKE concat( `name`, '%' ) 
	AND (capital != `name`)

SELECT
	`name`,
	capital 
FROM
	world 
WHERE
	capital LIKE concat( `name`,
						 '_%')

5.“Monaco-Ville"是合併國家名字 “Monaco” 和延伸詞”-Ville". 顯示國家名字,及其延伸詞,如首都是國家名字的延伸。

你可以使用SQL函數 REPLACE 或 MID.

SELECT
	`name`,
	REPLACE ( capital, `name`, '' ) AS ext 
FROM
	world 
WHERE
	capital LIKE concat( `name`, '%' ) 
	AND (capital != `name`)

SELECT from WORLD Tutorial

1.顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。

SELECT name, population, area FROM world 
WHERE (area > 3000000 AND population < 250000000) OR(area < 3000000 AND population > 250000000)

2.round

ROUND(7253.86, 0)    ->  7254
ROUND(7253.86, 1)    ->  7253.9
ROUND(7253.86,-3)    ->  7000

3.Show the name - but substitute Australasia for Oceania - for countries beginning with N.

SELECT name
       ,CASE WHEN continent='Oceania' 
             THEN 'Australasia'
             ELSE continent 
        END
FROM world
WHERE name LIKE 'N%'

4.Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B

SELECT name
       ,CASE WHEN continent='Asia' 
             THEN 'Eurasia'
             WHEN continent='Europe' 
             THEN 'Eurasia'
             WHEN continent = 'North America' 
             THEN 'America'
             WHEN continent = 'South America' 
             THEN 'America'
             WHEN continent = 'Caribbean' 
             THEN 'America'
             ELSE continent
        END
FROM world
WHERE name LIKE 'A%' OR name LIKE 'B%'

SELECT from Nobel Tutorial

1.Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

显示1984年获奖者和主题(按主题和获奖者名称排序); 但最后化学和物理排在最后。
The expression subject IN (‘Chemistry’,‘Physics’) can be used as a value - it will be 0 or 1.

SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner

Nobel Quiz

1.顯示有多少年沒有頒發醫學獎。

SELECT COUNT(DISTINCT yr) 
FROM nobel
WHERE yr 
NOT IN (SELECT DISTINCT yr 
		FROM nobel 
		WHERE subject = 'Medicine')

2.選擇代碼以顯示哪一年沒有頒發物理獎,亦沒有頒發化學獎。

SELECT yr FROM nobel
WHERE yr 
NOT IN(SELECT yr 
        FROM nobel
        WHERE subject IN ('Chemistry','Physics'))

SELECT within SELECT Tutorial

1.在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)

SELECT continent, name, area 

你可能感兴趣的:(SQL,sql)