2020-08-21 easy 1-6

1. Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+

| Id | Salary |

+----+--------+

| 1  | 100    |

| 2  | 200    |

| 3  | 300    |

+----+--------+


SELECT

            (

            SELECT DISTINCT `salary`

            FROM employee

            ORDER BY `salary` DESC

            LIMIT 1

            OFFSET 1

            ) AS sec_highest_salary

;

2. Combine Two Tables

Table: Person

+-------------+---------+

| Column Name | Type    |

+-------------+---------+

| PersonId    | int    |

| FirstName  | varchar |

| LastName    | varchar |

+-------------+---------+

PersonId is the primary key column for this table.

Table: Address

+-------------+---------+

| Column Name | Type    |

+-------------+---------+

| AddressId  | int    |

| PersonId    | int    |

| City        | varchar |

| State      | varchar |

+-------------+---------+

AddressId is the primary key column for this table.


Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people: FirstName, LastName, City, State

SELECT p.firstname, 

                p.lastname, 

                a.city, 

                a.state

FROM person AS p

LEFT JOIN address AS a

ON p.personid = a.personid

GROUP BY p.firstname, p.lastname, a.city, a.state

;

这个题address里面的perosnid并不是primary key, 所以存在一种情况是相同的personid有不同的address,所以我们join以后,其实personid有可能是有重复值的,为了避免这种情况发生,我们把最后要select的这些column全部group by以后再选,去掉所有重复值。


3. Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+

| Id | Name  | Salary | ManagerId |

+----+-------+--------+-----------+

| 1  | Joe  | 70000  | 3        |

| 2  | Henry | 80000  | 4        |

| 3  | Sam  | 60000  | NULL      |

| 4  | Max  | 90000  | NULL      |

+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers.

SELETC a.name

FROM employee AS a #员工表和员工salary

INNER JOIN employee AS b #manager表和manager的salary

ON a.managerid = b.id

WHERE a.salary > b.salary

;

join时要明确每个表代表的不同立场,可以通过注释来防止遗忘和混乱。


4. Reformat Department Table

Table: Department

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| id            | int    |

| revenue      | int    |

| month        | varchar |

+---------------+---------+

(id, month) is the primary key of this table.

The table has information about the revenue of each department per month.

The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].


Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.

Department table:

+------+---------+-------+

| id  | revenue | month |

+------+---------+-------+

| 1    | 8000    | Jan  |

| 2    | 9000    | Jan  |

| 3    | 10000  | Feb  |

| 1    | 7000    | Feb  |

| 1    | 6000    | Mar  |

+------+---------+-------+


SELECT

            id,

            SUM(CASE WHEN month = 'jan' THEN revenue ELSE NULL END) AS jan_revenue,

            SUM(CASE WHEN month = 'feb' THEN revenue ELSE NULL END) AS feb_revenue,

            SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS mar_revenue,

            SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS apr_revenue,

            SUM(CASE WHEN month = 'may' THEN revenue ELSE NULL END) AS may_revenue,

            SUM(CASE WHEN month = 'jun' THEN revenue ELSE NULL END) AS jun_revenue,

            SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS jul_revenue,

            SUM(CASE WHEN month = 'aug' THEN revenue ELSE NULL END) AS aug_revenue,

            SUM(CASE WHEN month = 'sep' THEN revenue ELSE NULL END) AS sep_revenue,

            SUM(CASE WHEN month = 'oct' THEN revenue ELSE NULL END) AS oct_revenue,

            SUM(CASE WHEN month = 'nov' THEN revenue ELSE NULL END) AS nov_revenue,

            SUM(CASE WHEN month = 'dec' THEN revenue ELSE NULL END) AS dec_revenue

FROM department

GROUP BY id

;

该题有两点需要注意的地方:一是当用group by和case when的时候,注意聚合函数应作用于整个case到end的语句,而不是放在then的后面;二是如果case when只想表达如果那么的关系,并没有涉及到else的话,这时候else也是可以加的(后面跟null),效果没有差别。


5. Customers Who Never Order

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

+----+-------+

| Id | Name  |

+----+-------+

| 1  | Joe  |

| 2  | Henry |

| 3  | Sam  |

| 4  | Max  |

+----+-------+

Table: Orders.

+----+------------+

| Id | CustomerId |

+----+------------+

| 1  | 3          |

| 2  | 1          |

+----+------------+


SELECT name

FROM customers

WHERE id NOR IN (SELECT DISTINCT customerid FROM orders)

;

这道题有个需要注意的地方:当表中含有多个id的时候,千万要注意id跟id之间是不是指同一个id,比如这道题中很容易就把两个表中的id当是一个id。


6. Duplicate Emails

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+

| Id | Email  |

+----+---------+

| 1  | a@b.com |

| 2  | c@d.com |

| 3  | a@b.com |

+----+---------+


SELECT a.email

FROM

(SELECT email, count(email) as num

FROM person

GROUP BY email) AS a

WHERE num >1

;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342