SQL实用题型-计算工作经历为先在谷歌后在亚马逊的人数

面试货拉拉被问到的sql题,当时没想出来,回来的时候突然想到了,可能会有更好的解决方法,这里介绍一种笨方法,题目如下:
给定表table1 如下:

id company year
A Google 2016
A Amazon 2017
A IBM 2018
B Google 2016
B Amazon 2019
C Google 2016
C IBM 2017

想到的方法是使用 With as 的方法,现将id和年份提取分别到满足“Google”和“Amazon”
下的两张子表,然后用join on id,再使用where子句进行条件筛选

WITH A AS (SELECT id,year as y1 FROM table1 WHERE company = "Google")
WITH B AS (SELECT id,year as y2 FROM table1 WHERE company = "Amazon")
SELECT id FROM A JOIN B ON id WHERE y1 > y2

你可能感兴趣的:(SQL实用题型整理)