Questions tagged [subquery]

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.

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.

Basic example:

SELECT f.column, (SELECT b.col FROM BAR b) AS col2
FROM FOO f

Correlated:

SELECT f.column, (SELECT b.col FROM BAR b WHERE b.col2 = f.col2) AS col2
FROM FOO f

Derived Table/Inline View:

SELECT f.*
FROM (SELECT t.* FROM FOOBAR t) AS f

IN/NOT IN:

SELECT f.*
FROM FOO f
WHERE f.column IN (SELECT b.col FROM BAR b)


SELECT f.*
FROM FOO f
WHERE f.column NOT IN (SELECT b.col FROM BAR b)

EXISTS/NOT EXISTS:

SELECT f.*
FROM FOO f
WHERE EXISTS (SELECT NULL FROM BAR b WHERE b.col = f.column)


SELECT f.*
FROM FOO f
WHERE NOT EXISTS (SELECT NULL FROM BAR b WHERE b.col = f.column)
709 questions
56
votes
8 answers

Get multiple columns from a select subquery

SELECT *, p.name AS name, p.image, p.price, ( SELECT ps.price FROM product_special ps WHERE p.id = ps.id AND ps.date < NOW() ORDER BY ps.priority ASC, LIMIT 1 ) AS special_price, ( …
Sparctus
  • 663
  • 2
  • 7
  • 8
52
votes
4 answers

Using column alias in a WHERE clause doesn't work

Given a table users with two fields: id and email. select id, email as electronic_mail from ( select id, email from users ) t where electronic_mail = '' Postgres complains that: ERROR: column "electronic_mail" does not…
Victor
  • 657
  • 1
  • 7
  • 11
35
votes
3 answers

Why does this query work?

I have two tables, table_a (id, name) and table_b (id), let's say on Oracle 12c. Why does this query not return an exception? select * from table_a where name in (select name from table_b); From what I understand, Oracle sees this as select * from…
eagerMoose
  • 521
  • 1
  • 5
  • 11
34
votes
1 answer

Postgres error [column must appear in the GROUP BY clause or be used in an aggregate function] when sub query is used

I have two tables employee and phones. An employee can have 0 to n phone numbers. I want to list the employee names with their phone numbers. I am using the below query which runs fine. SELECT empname,array_agg(phonenumber) AS phonenumbers FROM…
Programmer
  • 443
  • 2
  • 5
  • 6
31
votes
2 answers

Subqueries' aliases same as main queries' aliases

I have an SQL query whose aliases are the same as some of its subquery's aliases. For example: select * from ROOM r where ... ( select * from ROAD r where ... ) This works fine, as the…
IcySnow
  • 517
  • 3
  • 6
  • 7
26
votes
2 answers

SELECTing multiple columns through a subquery

I am trying to SELECT 2 columns from the subquery in the following query, but unable to do so. Tried creating alias table, but still couldn't get them. SELECT DISTINCT petid, userid, (SELECT MAX(comDate) FROM comments WHERE petid=pet.id) AS…
BufferStack
  • 393
  • 1
  • 3
  • 8
17
votes
1 answer

Why does subquery use parallelism and join doesn't?

Why does SQL server use parallelism when running this query which uses a subquery but it doesn't when using a join? The join version runs in serial and takes around 30 times longer to complete. Join version: ~30secs Subquery version:…
Chris L
  • 295
  • 1
  • 9
16
votes
3 answers

Slow update on large table with subquery

With SourceTable having >15MM records and Bad_Phrase having >3K records, the following query takes almost 10 hours to run on SQL Server 2005 SP4. UPDATE [SourceTable] SET Bad_Count= ( SELECT …
15
votes
1 answer

Poor performing subquery with date comparisons

When using a subquery to find the total count of all prior records with a matching field, the performance is terrible on a table with as little as 50k records. Without the subquery, the query executes in a few milliseconds. With the subquery, the…
15
votes
4 answers

MySQL subquery slows down drastically, but they work fine independently

Query 1: select distinct email from mybigtable where account_id=345 takes 0.1s Query 2: Select count(*) as total from mybigtable where account_id=123 and email IN () takes 0.2s Query 3: Select count(*) as total…
Stewie
  • 415
  • 1
  • 4
  • 10
15
votes
2 answers

How can SQL Delete using a sub query

The following code was added by one of our developers to delete duplicate records from the table: DELETE SubQuery FROM ( SELECT ID ,FK1 ,FK2 ,CreatedDateTime ,ROW_NUMBER() OVER(PARTITION BY FK1,…
Greg
  • 3,292
  • 5
  • 33
  • 57
15
votes
3 answers

PostgreSQL query very slow when subquery added

I have a relatively simple query on a table with 1.5M rows: SELECT mtid FROM publication WHERE mtid IN (9762715) OR last_modifier=21321 LIMIT 5000; EXPLAIN ANALYZE output: Limit (cost=8.84..12.86 rows=1 width=8) (actual time=0.985..0.986 rows=1…
P.Péter
  • 911
  • 1
  • 9
  • 20
13
votes
1 answer

Call function where argument is a (sub)select statement

My function takes an int4 as argument and returns a table: SELECT * FROM test_function(545421); -- works fine SELECT * FROM test_function(SELECT customerid FROM tableX where id = 1); -- syntax error How can I make this…
Beig
  • 245
  • 1
  • 2
  • 6
11
votes
3 answers

SQLITE: A problem of tags and products

I'm searching for a way to create a query to do the following: Let's consider 3 tables: products: List of products tags: list of tags tag_ties: table used to associate a tag to a product Let's consider this structure for each table: Products: id…
Julien L
  • 213
  • 2
  • 6
11
votes
1 answer

MySQL: Optimize UNION with "ORDER BY" in inner queries

I just set up a logging system which consists of multiple tables with the same layout. There is one table for each data source. For the log viewer, I want to UNION all the log tables, filter them by account, add a pseudo column for identification…
Lukas
  • 213
  • 1
  • 2
  • 10
1
2 3
47 48