SQL Server Data Query-complex query

Source: Internet
Author: User

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

Example 1 query the status of each student and their optional courses

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

The code is as follows: Copy code

Select student. *, SC. * from SC, student where SC. SNO = STUDENT. SNO;

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

Use a natural connection to connect to Example 1

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

The code is as follows: Copy code
Select student. SNAME, STUDENT. SSEX, STUDENT. SAGE, STUDENT. SDEPT, SC .*
From SC, STUDENT
Where SC. SNO = STUDENT. SNO;

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

Query the indirect preference of each course

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

 

The code is as follows: Copy code
Select sy. CNO, SX. cpno from course sx, course sy where sx. CNO = SY. CPNO

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

Connect to instance 1 with left outer join

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

The code is as follows: Copy code
Select student. SNAME, STUDENT. SSEX, STUDENT. SAGE, STUDENT. SDEPT, SC .*
From student left join SC on (SC. SNO = STUDENT. SNO );

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

Query the student's student ID, name, elective course name, and score ************************** *******************/

The code is as follows: Copy code

Select student. SNO, STUDENT. SNAME, COURSE. CNAME, COURSE. CCREDIT
From SC, STUDENT, COURSE
Where SC. CNO = COURSE. cno and SC. SNO = STUDENT. SNO;

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

Query students in the same department as Liu Chen

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

The code is as follows: Copy code

Select sx .*
FROM STUDENT SX
Where sx. sdept in (
Select sy. sdept from student sy where sy. SNAME = 'Liu Chen'
);
/************

Or:

The code is as follows: Copy code

Select sx .*
From student sx, STUDENT SY
Where sx. SDEPT = SY. sdept and sy. SNAME = 'Liu Chen'; or:

SELECT *
FROM STUDENT SX
Where exists (
SELECT * from student sy where sy. SDEPT = SX. sdept and sy. SNAME = 'Liu Chen'
);**************/

 

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

Query the student ID and name ************************** *******************/

 

The code is as follows: Copy code
Select sno, SNAME
FROM STUDENT
Where sno in (
SELECT SNO FROM SC
Where cno in (
SELECT CNO FROM COURSE
Where cname = 'information system'
                    )
);

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

Find the course number of each student that exceeds the average score of his or her elective course

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

 

The code is as follows: Copy code

Select sx. SNO, SX. CNO
FROM SC SX
Where sx. GRADE> (
Select avg (GRADE)
FROM SC SY
Where sx. SNO = SY. SNO
);

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

Query the names and ages of a student younger than a student in another computer science department.

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

The code is as follows: Copy code


Select sname, SAGE
FROM STUDENT
Where sage <ANY (
SELECT SAGE
FROM STUDENT
Where sdept = 'CS'
                    )
And sdept <> 'CS ';

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

Query the names and ages of students younger than all students in the computer science department.

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

The code is as follows: Copy code

Select sname, SAGE
FROM STUDENT
Where sage <ALL (
SELECT SAGE
FROM STUDENT
Where sdept = 'CS'
                    )
And sdept <> 'CS ';

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

Query the names of all students who have selected course 1.

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

The code is as follows: Copy code


SELECT SNAME
FROM STUDENT
Where exists (
SELECT * FROM SC
Where SC. SNO = STUDENT. sno and SC. CNO = '1'
);

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

Query the names of students who have selected all courses

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

The code is as follows: Copy code

SELECT SNAME
FROM STUDENT
Where not exists (
SELECT *
FROM COURSE
Where not exists (
SELECT *
FROM SC
Where student. SNO = SC. sno and course. CNO = SC. CNO
                                        )
);

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

Query the number of all students whose courses are selected at least 200215122

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

The code is as follows: Copy code

SELECT DISTINCT SNO
FROM SC X
Where not exists (
SELECT *
FROM SC Y
Where sno = '200' and not exists (
SELECT *
FROM SC Z
Where z. SNO = X. sno and y. CNO = Z. CNO
                                                           )
);

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

Select a record from the table, modify a field, and insert it back to the table.

Here, insert into can be used to INSERT subqueries ******************************* **************************/

The code is as follows: Copy code

Insert into course (CNO, CNAME, CPNO, CCREDIT)
SELECT '8', CNAME, CPNO, ccredit from course where cno = '2 ';

Related Article

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.