Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 613 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested select statement in SQL Server

#1
Why doesn't the following work?

SELECT name FROM (SELECT name FROM agentinformation)

I guess my understanding of SQL is wrong, because I would have thought this would return the same thing as

SELECT name FROM agentinformation

Doesn't the inner select statement create a result set which the outer SELECT statement then queries?
Reply

#2
You need to alias the subquery.

SELECT name FROM (SELECT name FROM agentinformation) a

or to be more explicit

SELECT a.name FROM (SELECT name FROM agentinformation) a
Reply

#3
The [answer](

[To see links please register here]

) provided by Joe Stefanelli is already correct.

SELECT name FROM (SELECT name FROM agentinformation) as a

We need to make an alias of the subquery because a query needs a table object which we will get from making an alias for the subquery. Conceptually, the subquery results are substituted into the outer query. As we need a table object in the outer query, we need to make an alias of the inner query.

Statements that include a subquery usually take one of these forms:

- WHERE expression [NOT] IN (subquery)
- WHERE expression comparison_operator [ANY | ALL] (subquery)
- WHERE [NOT] EXISTS (subquery)

Check for more [subquery rules](

[To see links please register here]

) and [subquery types](

[To see links please register here]

).

[More examples](

[To see links please register here]

) of Nested Subqueries.

1. IN / NOT IN – This operator takes the output of the inner query after the inner query gets executed which can be zero or more values and sends it to the outer query. The outer query then fetches all the matching [IN operator] or non matching [NOT IN operator] rows.

2. ANY – [>ANY or <ANY] – The >ANY operator takes the list of values produced by the inner query and fetches all the values which are greater than the minimum value of the list. The <ANY operator takes the list of values produced by the inner query and fetches all the rows which are less than the maximum value of the list.

e.g. >ANY(100,200,300), the ANY operator will fetch all the values greater than 100.

3. ALL – [>ALL or <ALL] – The >ALL operator takes the list of values produced by the inner query and fetches all the values which are greater than the maximum of the list. The <ALL operator takes the list of values produced by the the inner query and fetches all the rows which are less than the minimum value of the list.

e.g. >ALL(100,200,300), the ALL operator will fetch all the values greater than 300.

4. EXISTS – The EXISTS keyword produces a Boolean value [TRUE/FALSE]. This EXISTS checks the existence of the rows returned by the sub query.
Reply

#4
TRY THIS

'select *,(SELECT count(id) FROM products WHERE user_id = users.id) as products_count from users ORDER BY products_count DESC, ID DESC LIMIT 200
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through