DAY 5 kejian

Sub Query
Independent Sub Query
Correlated Query
Exists and Not Exists
A sub-query is a query within a query.

 A sub-query answers the queries that have multiple parts

 The sub-query answers one part of the question

The sub query is enclosed in parentheses

List the name of item whose unit price is greater than the unit price of item id ‘BAK101’
SELECT itemname  FROM item
 WHERE unitprice
  >
 (SELECT unitprice FROM item WHERE itemid=‘BAK101’ );

 

List the details of the items whose unit price is maximum.
item
itemid  itemname unitprice  class
SELECT itemid, itemname, unitprice
  FROM item
        WHERE unitprice 
   =
 (SELECT MAX(unitprice) FROM item);    //Get the MAXimum Unit Price available in the item table


List the second highest Unit price from the item table
SELECT MAX(unitprice)
  FROM item
        WHERE unitprice 
   !=  //Remove the highest Unit Price
 (SELECT MAX(unitprice) FROM item); //Get the highest Unit Price


List the details of the item whose unit price is more than the average Unit price of items.
SELECT itemid,itemname,unitprice
  FROM item
        WHERE unitprice 
   >
 (SELECT AVG(unitprice) FROM item);   //Get the average Unit Price


List the customerid and SUM of total purchase amount  for those customers whose SUM of  total purchase amount is

more than the average  of the SUM of total purchase amount  of all the customers.
customerpurchase
customerid   itemid   qtypurchased   billid   netprice
SELECT customerid,SUM(netprice)
  FROM customerpurchase
              GROUP BY customerid
        HAVING SUM(netprice) 
   >
 (SELECT AVG(SUM(netprice)) FROM customerpurchase GROUP BY customerid);  //Get the average of

SUM of Net price  of all the purchases

 

customerid  itemid  qtypurchased  billid  netprice
C1 STN001 5 1001 150
C2 GRO001 1 1002 10
C1 ELC001 1 1001 5000
C2 STN002 2 1002 400
C3 STN002 2 1003 400
After grouping  by customer id and finding SUM of net Price for each group
customerid  SUM(netprice)
C1 5150
C2 410
C3 400
After finding average of SUM of net price for each group
AVG(SUM(netprice))
1986.666667


List the details of the customer(s) who have paid a bill of maximum amount
customer
customerid  customername  dateofreg  userid  password

SELECT customerid, customername FROM customer
              WHERE customerid
   IN
   (SELECT customerid FROM customerpurchase
       GROUP BY billid, customerid HAVING SUM(netprice)   //Get the customer id(s) who have paid the maximum bill. Asuming one bill will be paid by only one customer
   =
(SELECT MAX(SUM(netprice)) FROM customerpurchase GROUP BY billid));  //Get the maximum  of SUM of Net price  of all the bill ids


customerpurchase
customerid  itemid  qtypurchased  billid  netprice
C1 STN001 5 1001 150
C2 GRO001 1 1002 10
C1 ELC001 1 1001 5000
C2 STN002 2 1002 400
C3 STN002 2 1003 400

After grouping  by billidand finding SUM of net Price for each group
billid  SUM(netprice)
1001  5150
1002  410
1003  400
After finding maximum of SUM of net price for each group
MAX(SUM(netprice))
5150
You can refer to the table in the FROM clause of the outer query in the inner query using Correlated sub-queries.

The inner query is executed separately for each row of the outer query.
 (i.e. In Co-Related Sub-queries, SQL performs a sub-query over and over again – once for each row of the main query. )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值