Sub Query

old/SQL 2010. 3. 16. 14:58

1) WHERE 절에서 사용

SELECT * FROM tbl_cake WHERE price >= (SELECT AVG(price) FROM tbl_cake);


2) HAVING 절에서 사용

SELECT code, MIN(arrival) FROM tbl_stock GROUP BY code
            HAVING MIN(arrival) < (SELECT AVG(shipment) FROM tbl_stock);



3) FROM 절에서 사용

SELECT MIN(price)
            FROM (SELECT * FROM tbl_cake WHERE price >= 2500) AS c_price;



* 응용

INSERT INTO tbl_advance SELECT * FROM tbl_results
    WHERE point1 + point2  > (SELECT AVG(point1 + point2) FROM tbl_results);

UPDATE tbl_allowance SET total = (overtime + travel)
     WHERE overtime + travel < (SELECT MAX(overtitme) FROM tbl_allowance);

DELETE FROM tbl_allowance WHERE travel > (SELECT AVG(travel)
            FROM tbl_allowance WHERE overtime >= 40000);

'old > SQL' 카테고리의 다른 글

VIEW  (0) 2010.03.17
JOIN  (0) 2010.03.17
DELETE  (0) 2010.03.16
UPDATE  (0) 2010.03.16
INSERT  (0) 2010.03.16
Posted by jazzlife
,