Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 993

SQL term used to describe when a `SELECT` statement is used as part of a larger SQL statement. The larger statement may be DML and is always found within brackets or parenthesis.

3 votes
Accepted

Subquerying results in no row returned although records exist

One of the subqueries (derived tables) returns 0 rows and thus the whole query - which is a cross join of 5 tables - returns 0 rows as well. You could rewrite with proper, explicit joins. If you make …
ypercubeᵀᴹ's user avatar
1 vote

Subselect dependent on first select value

You can use 2 subqueries, one joining with the contact_id and another joining with the alt_contact_id. Then UNION them and ORDER BY: SELECT cont.*, cred.*, 'main' AS contact_type FROM e_contacts AS c …
ypercubeᵀᴹ's user avatar
4 votes

Using max for each sub group does not seem to work.

You have two right parenthesis more than left ones and a missing alias: Select * from wh_acct a -- alias added where a.rundate = (select max(a2.rundate) …
ypercubeᵀᴹ's user avatar
1 vote
Accepted

How to INNER JOIN and OUTER JOIN within the same table?

The query is joining 2 tables (receipts, sales) that both have a many-to-one relationship with product. This creates a kind of cartesian (cross) product and will give wrong results in the SUM() calcul …
ypercubeᵀᴹ's user avatar
1 vote
Accepted

Query for existence of objects across two multi-to-one relationships

You can add this restriction with either a NOT EXISTS subquery or with LEFT JOIN / IS NULL: SELECT * FROM MyObject INNER JOIN ( SELECT * from ConfigKV WHERE key = 'foo' AND value = 'bar' ) as SQ1 on …
ypercubeᵀᴹ's user avatar
4 votes
Accepted

Correlated sub-query on the same table with only one alias

The standard behaviour is that in WHERE id = ... in the subquery the id refers to the closest table in scope. And that is t2. … You are lucky though because the WHERE id = t2.id - which is translated as WHERE t2.id = t2.id is not actually needed because you used WHERE id IN (subquery). …
ypercubeᵀᴹ's user avatar
4 votes

Are these two queries technically equal to each other?

No, they are not equivalent. If Confirmed_Attendance.rsCode is not nullable, then your second query is equivalent to this one: SELECT cs.RSCode , CustomerName , em.EmailId FROM Cu …
ypercubeᵀᴹ's user avatar
2 votes

Not getting the expected result with 'IN' and subquery

First, fix your schema. As soon as possible. There is absolutely no reason to be storing comma separated lists in a table column. Quite the opposite, there a lot of reasons not to. See these answers f …
ypercubeᵀᴹ's user avatar
1 vote
Accepted

Use an Alias in Where Clause Subquery

You can reference a column in a inline subquery but not in a derived table inside an inline subquery. … This would require either using ROW_NUMBER() and encapsulating the whole query ina derived table or turning the inline subquery into a LEFT JOIN. …
ypercubeᵀᴹ's user avatar
1 vote

Query keeps on executing

Then we have the second part, the subquery with table2. …
ypercubeᵀᴹ's user avatar
2 votes
Accepted

Query to find missing status events

@spthorn's answer is correct but IN gives sometimes unexpected results when the columns involved are nullable. If you want to avoid such surprises, it's better to use NOT EXISTS or the LEFT JOIN / IS …
ypercubeᵀᴹ's user avatar
2 votes

correlate subquery in hive

The syntax of you query is not often called "correlated subquery". … A correlated subquery would be like this: SELECT ndo.sku AS ParentSKU, ( SELECT SUM(g.sessions) FROM gasessiondata AS g WHERE g.date >= ndo.FromDate AND g.date …
ypercubeᵀᴹ's user avatar
0 votes

In MySQL, get number of pregnant women who received 3 ANC checkups

This would also work: SELECT COUNT(*) FROM ans_checkups WHERE (SELECT visit1_date AS visit UNION ALL SELECT visit2_date UNION ALL SELECT visit3_date UNION ALL …
ypercubeᵀᴹ's user avatar
13 votes
Accepted

How to get all records from one table and see if they are used in another without a sub query

Note how the WHERE condition of the subquery was moved to the ON clause of the join: SELECT uc.UserContractId, uc.UserContractName, oc.OrderID FROM tblUserContracts AS uc LEFT JOIN …
ypercubeᵀᴹ's user avatar
5 votes
Accepted

Very weird result of CTEs and subqueries

In the last, external query you cross join foo with t1 and t2, while you only need (and use) columns from foo and t2. So remove the t1 join: WITH ... SELECT FROM ( ... ) foo, t2 ; …
ypercubeᵀᴹ's user avatar

1
2 3 4 5 6
15 30 50 per page