Advanced query for SQL database

Source: Internet
Author: User

1. Connection Query

SELECT * FROM Info,nation--to form a Cartesian product

SELECT * from info,nation where info.nation = Nation.code

Select Info.code,info.name,sex,nation.name,birthday from info,nation where info.nation = Nation.code

SELECT * FROM Info join Nation in info.nation = Nation.code--join on form

2. Joint queries

Select Code,name from Info
Union
Select Code,name from Nation

3. Sub-query

An SQL statement contains two queries, one of which is the parent query (the outer query), the other is a subquery (the inner query), and the result of the subquery query is the condition of the parent query.

--Query the people for the Han nationality of all personnel information
SELECT * from Info where Nation = (select Code from Nation where Name = ' Han ')


(1) Unrelated subqueries

Subqueries can be executed independently, and the subquery does not have a relationship with the parent query

--Inquiry series is all the car information of BMW 5 system
SELECT * from Car where brand = (select Brand_Code from Brand where brand_name = ' BMW 5 Series ')


(2) Related sub-query

--find cars with fuel consumption below the average fuel consumption of the series

SELECT * from Car where oil< (average fuel consumption for this series)
Select AVG (oil) from Car where Brand = (this series)


SELECT * from Car a where oil< (select Avg. from car b where B.brand = A.brand)

Advanced query for SQL database

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.