Thursday, June 30, 2011

SQL Subquery

Subquery or Inner query or Nested query is a query in a query. A subquery is usually added in the WHERE Clause of the sql statement. Most of the time, a subquery is used when you know how to search for a value using a SELECT statement, but do not know the exact value. Subqueries are an alternate way of returning data from multiple tables. Subqueries can be used with the following sql statements along with the comparision operators like =, <, >, >=, <= etc. SELECT INSERT UPDATE DELETE For Example: 1) Usually, a subquery should return only one record, but sometimes it can also return multiple records when used with operators like IN, NOT IN in the where clause. The query would be like, SELECT first_name, last_name, subject FROM student_details WHERE games NOT IN ('Cricket', 'Football'); The output would be similar to: first_name last_name subject ------------- ------------- ---------- Shekar Gowda Badminton Priya Chandra Chess 2) Lets consider the student_details table which we have used earlier. If you know the name of the students who are studying science subject, you can get their id's by using this query below, SELECT id, first_name FROM student_details WHERE first_name IN ('Rahul', 'Stephen'); but, if you do not know their names, then to get their id's you need to write the query in this manner, SELECT id, first_name FROM student_details WHERE first_name IN (SELECT first_name FROM student_details WHERE subject= 'Science'); Output: id first_name -------- ------------- 100 Rahul 102 Stephen In the above sql statement, first the inner query is processed first and then the outer query is processed. 3) Subquery can be used with INSERT statement to add rows of data from one or more tables to another table. Lets try to group all the students who study Maths in a table 'maths_group'. INSERT INTO maths_group(id, name) SELECT id, first_name || ' ' || last_name FROM student_details WHERE subject= 'Maths' 4) A subquery can be used in the SELECT statement as follows. Lets use the product and order_items table defined in the sql_joins section. select p.product_name, p.supplier_name, (select order_id from order_items where product_id = 101) as order_id from product p where p.product_id = 101 product_name supplier_name order_id ------------------ ------------------ ---------- Television Onida 5103 Correlated Subquery A query is called correlated subquery when both the inner query and the outer query are interdependent. For every row processed by the inner query, the outer query is processed as well. The inner query depends on the outer query before it can be processed. SELECT p.product_name FROM product p WHERE p.product_id = (SELECT o.product_id FROM order_items o WHERE o.product_id = p.product_id);

1 comment:

  1. http://eresult.co/10th-board-result-of-rajasthan-board-announced-on-18th-june-expected/
    http://eresult.co/2011-tcs-technical-interview-questions/
    http://eresult.co/aptitude-paper-with-solutions-for-mncs-set-1/
    http://eresult.co/aptitude-paper-with-solutions-for-mncs-set-2/
    http://eresult.co/aptitude-paper-with-solutions-for-mncs-set-3/
    http://eresult.co/aptitude-questions-and-answer-for-placement/
    http://eresult.co/bca-3rd-eresult-bhavnagar-university/
    http://eresult.co/bca-3rd-year-final-exam-eresult-of-bhavnagar-university/
    http://eresult.co/board-of-secondary-education-odisha/
    http://eresult.co/board-of-secondary-education-odisha-online-registeration-of-teachers/
    http://eresult.co/gtu-ccc-exam-result-07-09-2015-to-18-09-2015-dob-wise-eresult-2015/
    http://eresult.co/hp-university-gazette-notification-of-bsc-nursing/
    http://eresult.co/hp-university-gazette-notification-of-post-basic-bsc-nursing-exam-result-sep-2015/
    http://eresult.co/java-collection-interview-questions/
    http://eresult.co/java-collection-interview-questions-and-answers/
    http://eresult.co/java-interview-questions-set-1/
    http://eresult.co/java-interview-questions-set-2/
    http://eresult.co/java-interview-questions-set-3/
    http://eresult.co/java-interview-questions-set-4/
    http://eresult.co/java-interview-questions-set-5/
    http://eresult.co/java-interview-questions-set-6/
    http://eresult.co/java-interview-questions-set-7/
    http://eresult.co/jsp-interview-questions-and-answers/
    http://eresult.co/national-eligibility-test-net-2016-eresult/
    http://eresult.co/oracle-interview-questions-and-answers-sql/
    http://eresult.co/php-interview-questions-and-answers-set-1/
    http://eresult.co/php-interview-questions-and-answers-set-10/
    http://eresult.co/php-interview-questions-and-answers-set-2/
    http://eresult.co/php-interview-questions-and-answers-set-3/
    http://eresult.co/php-interview-questions-and-answers-set-4/
    http://eresult.co/php-interview-questions-and-answers-set-5/
    http://eresult.co/php-interview-questions-and-answers-set-6/
    http://eresult.co/php-interview-questions-and-answers-set-7/
    http://eresult.co/php-interview-questions-and-answers-set-8/
    http://eresult.co/php-interview-questions-and-answers-set-9/
    http://eresult.co/sbi-clerk-exam-eresults-2016-to-be-declare/
    http://eresult.co/servlet-interview-questions-and-answers/
    http://eresult.co/software-tester-interview-questions-solutions-software-tester-jobs/
    http://eresult.co/software-testing-interview-questions-jobs/
    http://eresult.co/sql-interview-questions-and-answers/
    http://eresult.co/sql-interview-questions-and-answers-set-1/
    http://eresult.co/sql-interview-questions-and-answers-set-2/
    http://eresult.co/sql-interview-questions-and-answers-set-3/
    http://eresult.co/sql-server-interview-questions-and-answers/
    http://eresult.co/sql-server-interview-questions-set-2/
    http://eresult.co/sql-server-interview-questions-set-3/
    http://eresult.co/sql-server-interview-questions-set-4/
    http://eresult.co/struts-interview-questions-and-answers/
    http://eresult.co/technical-questions-and-answer-for-placement/
    http://eresult.co/university-of-kerala-mba-iii-semfulltimeregularevening-uim-and-part-time-exam-result-2015/

    ReplyDelete