| |

VerySource

 Forgot password?
 Register
Search
View: 4993|Reply: 24

A classic sql interview question! I think I have a good time playing with sql!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-20 13:30:02
| Show all posts |Read mode
Student ID (Automatic Numbering) Name Gender Age

0001 xw male 18

0002 mc female 16

0003 ww male 21

0004 xw male 18

Please write SQL statements that implement the following functions:
Delete redundant records with the same fields except the student number (automatic number) field!
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-27 22:15:01
| Show all posts
Method one, delete table where student number (automatic numbering) not in
(select max (student number (automatic number)) as xh, name, gender, age
  from table
group by name, gender, age) A
Method Two,
delete table where student number (automatic numbering) not in (
select max (a. student number) from table B, (select distinct name, gender, age from table) A
 where A. name = b.name and a.sex = b.sex and a.age = b.age
) C

The above method does not know whether it is possible, and has not been tested in SQL.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-28 07:15:01
| Show all posts
delete from tablename where student number (automatic numbering) notexists (select distinct name, gender, age from tablename)
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-28 09:00:01
| Show all posts
up,azuazuazupositive solution!
Reply

Use magic Report

0

Threads

14

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-28 14:30:01
| Show all posts
azuazuazu, there is no distinction between in and exists ...

The idea of ​​jieslieis correct, and the wording after debugging is as follows: (note that the sub query of in can only select one column)
DELETE FROM table1
WHERE (Student ID NOT IN
          (SELECT MAX (student number) AS xh
         FROM TABLE1
         GROUP BY name, gender, age))
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-29 21:45:01
| Show all posts
Positive Solutions Upstairs
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-1 09:30:02
| Show all posts
What is classic?
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-2 05:00:01
| Show all posts
Basic.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-6 09:30:01
| Show all posts
DELETE FROM Test2
where [学 号] in
(select [Student ID] = min ([Student ID]) from Test2 group by [Name], [Gender], [Age] having count (*)> 1)
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-21 22:00:01
| Show all posts
kcactus

This is definitely no problem. The idea is clear.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list