I mean I often use nested queries to solve the queries in SQL SERVER but I didn't definitely know about the reason of using them and sometimes it get so complicated that it is difficult to find the path between entities.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 27, 2015, 3:46 PM EDT
Stored Procedures Best Practices - CodeProject
Stored Procedure Generator For SQL Server - CodeProject
Overview of SQL
SQL Server – How to write stored procedures with output ...
Server Stored Procedure - CodeProject
Code First Stored Procedures - CodeProject
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 21, 2015, 3:25 PM EDT
SELECT * FROM ( SELECT A.Address, E.Name, E.Age From Address A Inner join Employee E on E.EID = A.EID) T WHERE T.Age > 50 ORDER BY T.NAMEThe query looks really a mess. Even if I need to write something that wraps around the entire query, it would gradually become unreadable. CTE allows you to generate Tables beforehand and use it later when we actually bind the data into the output.
Rewriting the query using CTE expressions would look like:
With T(Address, Name, Age) --Column names for Temporary table AS ( SELECT A.Address, E.Name, E.Age from Address A INNER JOIN EMP E ON E.EID = A.EID ) SELECT * FROM T --SELECT or USE CTE temporary Table WHERE T.Age > 50 ORDER BY T.NAMEYes as you can see, the second query is much more readable using CTE. You can specify as many query expressions as you want and the final query which will output the data to the external environment will eventually get reference to all of them.
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 21, 2015, 7:55 PM EDT
Munesh SharmaRank #174 · 13.9K reputationPosted May 21, 2015, 8:54 PM EDT
Harminder SinghRank #1,153 · 1.6K reputationPosted May 27, 2015, 6:26 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 27, 2015, 8:10 AM EDT
Subquery or Inner query or Nested query is a query in a query. SQL 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 in the database.
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.
EXAMPLE 1
Get the employee numbers and enter dates of all employees with enter dates equal to the earliest date:
EXAMPLE 2
Get the employee numbers, last names, and jobs for all employees who entered their projects on October 15, 2007:
SELECT p.product_name FROM product pWHERE p.product_id = (SELECT o.product_id FROM order_items o
WHERE o.product_id = p.product_id);
Nested Subquery
1) You can nest as many queries you want but it is recommended not to nest more than 16 subqueries in oracle
Non-Corelated Subquery
2) If a subquery is not dependent on the outer query it is called a non-correlated subquery
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 27, 2015, 8:34 AM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 21, 2015, 9:42 PM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 21, 2015, 9:46 PM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 27, 2015, 8:22 AM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 27, 2015, 9:17 AM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 30, 2015, 1:42 AM EDT