Three ways to use sub-queries

Source: Internet
Author: User

Three ways to use subqueries:

1, sub-query as condition

1 "If you are following > < >= <= =! = <>, you must ensure that the subquery returns only a single value, and if you return a column of multiple rows, you need to specify the range using in, and if you return a row of multiple columns, an error

Eg: (Multiple rows and one column)

SELECT * from Student where ClassId on (select ClassId from grade)

2, the subquery acts as a result set (you can replace the table behind the from with the virtual result set)

According to the primary key Studentno sort, row_number () will have the line number, over () Specify the Sort field

Select *,row_number () over (order by Studentno) from Student

eg://The virtual result set requires the Set alias (temp) to generate the line number also to specify the alias (as ID)

SELECT * FROM (select *,row_number () over (order by STUDENTNO) as ID from Student) temp where id>0 and id<=5//pagination

3, sub-query as the value of the column

Select (select Studentname from Studnet where

Student.studentno=result.studentno), Studentresult from Result

Three ways to use sub-queries

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.