---
title: "SQL常见笔试题"
author: "fairy_xzc"
date: "2017-09-06T23:00:20Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2017-09-06T22:56:33Z"
id: 2392433
url: "https://agilestyle.iteye.com/blog/2392433"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2392433](/blog/2392433)**

 

有一张数据表DDR

![](http://dl2.iteye.com/upload/attachment/0126/8514/a6bfafa1-84b4-3886-9857-21fd28fe7a8e.png)

问题：查询出T2重复出现1次以上的记录

![](http://dl2.iteye.com/upload/attachment/0126/8516/97c796de-a2b7-3eb2-8960-6566e68ab532.png)

SQL Reference

```
select * from ddr 
where t2 in 
(
	select t2 from ddr 
	group by t2 
	having count(t2) > 1
);
```

   
数据库2张表，一张Customer，字段id，name；一张Orders，字段id，cid（Customer的ID外键），revenue，问题：查询出每个Customer的revenue总和

![](http://dl2.iteye.com/upload/attachment/0126/8518/0ff0aca5-e87d-335b-9c0b-2543431a2b71.png)  
![](http://dl2.iteye.com/upload/attachment/0126/8520/5aadd115-465f-32c8-8921-33ed311bbcda.png)

SQL Reference

```
select sum(revenue), c.name
from orders o LEFT JOIN customer c on o.cid = c.id 
GROUP BY cid;
```

![](http://dl2.iteye.com/upload/attachment/0126/8522/557af3a4-34c6-3a5c-86ae-98b57a514206.png)