---
title: "出道distinct相关的sql题给大家做做"
author: "coderplay"
date: "2009-05-22T20:13:07Z"
tags: []
categories: ["数据库"]
slug: "distinct-sql"
draft: false
lastmod: "2009-05-22T21:21:13Z"
id: 393717
url: "https://coderplay.iteye.com/blog/393717"
---

这几天在做sql编译相关的东西, 自己弄了个题目,连资深数据库开发人员都可能会搞错. 以下sql中哪些执行时会报错? (适于所有常见DBMS)  
1\.

```
select
   distinct col
from
  tbl
```

2\.

```
select
  distinct col1,
  distinct col2
from
  tbl  
```

3\.

```
select
   count(distinct col% 10)
from
  tbl
```

 4.

```
select
   length(distinct col1% 10)
from
  tbl
```

 5.

```
select
   sum(distinct col1% 10),  
   sum(distinct col1% 9)
from
  tbl
```

 6.

```
select
  distinct col1,
  count(distinct col2)
from
  tbl
```

 

 7.

```
select
  sum(distinct col1 % 10),
  count(distinct col2 % 9)
from
  tbl
```

 8.

```
select
  max(distinct substr(col1, 1, 10)),
  count(distinct col2 % 9)
from
  tbl
```