SQL deletes duplicate rows and queries all statements that are larger than a certain score analysis

Source: Internet
Author: User

With such a problem, a SQL statement is used to query the names of students who have more than 80 points per course.

Here is the table

Analysis, query each course is more than 80 students. SELECT DISTINCT name from dbo.student WHERE fenshu<=80 The result of the query is John Doe Harry Zhang San have.

Statements less than or equal to 80 select name from Dbo.student WHERE fenshu<=80 only 31.

This is repeated, and it is worthwhile to use the words in the not-in (no-time) statement to spell the two sentences as follows:

SELECT DISTINCT name from dbo.student WHERE name is not in (
SELECT name from Dbo.student WHERE fenshu<=80)

Now there is only the desired result (note that the result of the query is three Harry, you need to use distinct to eliminate duplicate rows).

Second question: Delete the same student redundancy information except the automatic numbering, the table is as follows:

Do this problem we see the ID is not the same, the others are the same, then you need to group, the statement: SELECT MAX (ID) from the Dbo.student1 Group by Grade,kecheng,name,x_id,course Group, And get the maximum ID. The fields after the group by statement must be the same before a group is generated.

Delete a statement after grouping

DELETE from Dbo.student1 WHERE ID not in (
SELECT MAX (ID) from Dbo.student1 GROUP by Grade,kecheng,name,x_id,course)

Delete the time by the ID of the condition, not the sub-condition of the statement to delete, and finally get their own ideal table.

SQL deletes duplicate rows and queries all statements that are larger than a certain score analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.