SQL Statement Learning

Source: Internet
Author: User

Exercise for a table


1. Build a table

CREATE table student (name char, curriculum char, score char (20));

Insert data:

INSERT into student (Name,curriculum,score) VALUES (' Harry ', ' math ', ' 100 ');

Mysql> select * from student;

+--------+------------+-------+

| name | Curriculum | Score |

+--------+------------+-------+

| Zhang San | language | 81 |

| Zhang San | Math | 75 |

| John Doe | language | 76 |

| John Doe | Math | 90 |

| Harry | language | 81 |

| Harry | Math | 100 |

+--------+------------+-------+

2. Topic starts
(1) use an SQL statement to find out the names of students who are greater than the number of each course , and to understand the group by
Answer:
Select distinct name from student where name is not in (select distinct name from student where score<80);

+--------+

| name |

+--------+

| Harry |

+--------+


SELECT DISTINCT * FROM student where name is not in (select distinct name from student where score<80);

+--------+------------+-------+

| name | Curriculum | Score |

+--------+------------+-------+

| Harry | language | 81 |

| Harry | Math | 100 |

+--------+------------+-------+



SELECT DISTINCT * FROM student where name is not in (select distinct name from student where score<80) group by name;

+--------+------------+-------+

| name | Curriculum | Score |

+--------+------------+-------+

| Harry | language | 81 |

+--------+------------+-------+



(2) Form the following form

+--------+--------+--------+

| name | language | Math |

+--------+--------+--------+

| Zhang San | 81 | 75 |

| John Doe | 76 | 90 |

| Harry | 81 | 100 |

+--------+--------+--------+


Answer:



(3) Show if each section passes, use case when

+--------+------------+-------+-----------+

| name | Curriculum | Score | Pass |

+--------+------------+-------+-----------+

| Zhang San | language | 81 | Pass |

| Zhang San | Math | 75 | Fail |

| John Doe | language | 76 | Fail |

| John Doe | Math | 90 | Pass |

| Harry | language | 81 | Pass |

| Harry | Math | 100 | Pass |

+--------+------------+-------+-----------+


Answer:
Select Name, Curriculum,score, (case is student.score>=80 then ' pass ' ELSE ' fail ' END) as pass from  student;

(4) Order by score

+--------+------------+-------+

| name | Curriculum | Score |

+--------+------------+-------+

| Harry | Math | 100 |

| John Doe | Math | 90 |

| Zhang San | language | 81 |

| Harry | language | 81 |

| John Doe | language | 76 |

| Zhang San | Math | 75 |

+--------+------------+-------+


Answer: (+0 because it is char to int)

SELECT * FROM student ORDER by score+0 Desc;




/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/





SQL Statement Learning

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.