Search Results
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 |
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.
2
votes
How do I optimize this query that gets the newest entry of each group?
You can try the following query.
Adding an index on (tid, timestamp, id) should help with efficiency:
SELECT a.*
FROM
( SELECT DISTINCT tid
FROM ast
) AS t
LEFT JOIN ast AS a
ON …
4
votes
Doesn't T-SQL support correlated CROSS JOINs?
To answer the main question:
Doesn't T-SQL support correlated CROSS JOINs?
No. CROSS joins are not correlated in SQL (any variant, not only T-SQL).
Is there a workaround?
Yes. Correlated joins are …
2
votes
Limit left join for multiple rows
Another method that works even in ancient versions (eg 5.1) is to mimic the LATERAL join with what I call "poor man's" LATERAL JOIN / OUTER APPLY.
This is the LATERAL join we want to mimic:
SELECT i.* …
3
votes
Return all archive records when any one record meets criteria
The WHERE clause in the subquery is not correct:
the condition should be checking the subquery table (PTAA), not the main one (PTA), in order to get the correlated effect you are seeking.
minor correction …
1
vote
Accepted
LEFT JOIN subquery fails in the presence of an EXISTS clause in the main query
add a condition in b subquery:
LEFT JOIN (
/* ➊ Inner query */
SELECT eq.contact_id contact_id, COUNT(*) sent
FROM mailing_job j
INNER JOIN mailing_event_queue eq ON eq.job_id …
5
votes
Accepted
What am I doing wrong with subquery
There are many many ways to do this. Here is one that only slightly changes your attempt:
SELECT m1.ModuleID, m1.CWWeight, l.StaffID
FROM dbo.module as m1
INNER JOIN dbo.Lecturer as l
ON m1.Modul …
1
vote
Alternate to use LIMIT in subquery in mysql?
If 2 is a fixed value and you'll never need to compare against 3, 4 or more items in the future, then you could convert the subquery to a derived table and find the 2 values with aggregation:
select r.res_id …
2
votes
Accepted
Selecting data from a sub query
You need to move that COUNT() in the subquery.
Assuming you want to show all Events from the 3 continents with lowest count of events:
Select c.[ContinentName], a. …
11
votes
Accepted
Subquery gives no error for a non-existing column with the same name as in the outer query
I run this query:
select * from t1 where c1 in (select c1 from t2);
The above query should give an error as c1 is not present in t2.
Instead, it returns all the rows from t1.
No, the query should n …
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 …
3
votes
Accepted
How to make subtraction without repeating the subqueries?
You can use CTEs when you have repeating subqueries. Although it's not necessarily better for performance, in Postgres CTEs are an optimization fence, in other words they are evaluated once (or not at …
2
votes
Accepted
How to use GROUP BY in an efficient way without losing attributes?
The other common method would be using window functions in a subquery or CTE and then restricting the result with a WHERE in the main query - exactly what Lennart's answer does. … In any case, the problem needs some kind of subquery to be solved.
For efficiency, an index on (player_id) is very likely to help, with either method you write the query. …
3
votes
Accepted
Replace Subquery with JOIN - MYSQL
The trick is to rewrite the inline subquery as a join but not as a derived table. The subquery becomes the right part of the ON condition. …
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 ;
…
5
votes
Accepted
Deduplicate Data Using Having with Subquery
.*
FROM game AS g
LEFT JOIN
( SELECT min(id) AS id
FROM game
GROUP BY matchid, ordinal
) AS d
ON g.id = d.id
WHERE d.id IS NULL ;
or an EXISTS subquery rewritten as a JOIN
(delete …