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)
708 questions
53
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
  • 623
  • 2
  • 7
  • 8
5
votes
1 answer

does a subquery pull the entire table?

i am attempting to run a subquery in a table: SELECT t1.*, t2.* FROM t1 LEFT JOIN (SELECT * FROM table2 GROUP BY col1) AS t2 on t1.col1 = t2.col1 WHERE ... My main question is, since that subquery called on it's own would pull the entire time,…
dab
  • 51
  • 1
2
votes
1 answer

Correlated Subqueries: resolving ambiguous names

Suppose I have 2 tables which share some column names. In this case the primary key is both is called id. CREATE TABLE artists( id int primary key, name text, -- …, ); CREATE TABLE paintings( id int primary key, artistid references…
Manngo
  • 2,819
  • 8
  • 34
  • 56
2
votes
1 answer

Comparing Row Values

I'm a SQL newbie and would like some help please. This is my table: ID Name Number Src_Call_Leg Dest_Call_Leg -- ---- ------ ------------ ------------- 1 John 5555 100 1000 2 John 5555 100 102 The table…
1
vote
1 answer

Selecting data from multiple rows and insert it into a column

My table is like this: temp1 name city phone_no pincode ----------------------------------- amit Delhi 12345 123 Rajiv Bombay 836536 432 How do I transform the last three columns into a single column, address, like in…
Aditya
  • 11
  • 2
0
votes
1 answer

Querying All Records of All Users who have any instance of purchase behavior in their history

I am trying to write a query that shows me all user ids that in any instance of usage have a particular type of "purchase credit" associated with their account. I have one table, "users" that shows user information, ie, user ids, names, birthdate,…
0
votes
2 answers

calculating max and joining tables

Just need a little help as new to SQL. I have a table which contains readingtime (records every 4 seconds during the day) and an energy reading. I wanted to know how much energy was used in a day. this is the query that i used SELECT…
user46507
  • 1
  • 2
0
votes
2 answers

Is my subquery inneficient. It is extremely slow

I have two tables. Member information and login entries. I am simply trying to display the member information table sorted by the last login access date. So i created this query, but it is extremely slow. Is there a better way to write it? SELECT …
Nelson
  • 1
0
votes
0 answers

subqueries with select

This below query works as expected: SELECT reports.* FROM reports INNER JOIN units ON units.id = reports.unit_id WHERE reports.unit_id IN (1111, 1112, 1113) AND ( reports.id = ( …
JohnMerlino
  • 1,739
  • 5
  • 18
  • 21
0
votes
2 answers

Subquerying results in no row returned although records exist

I have two tables, namely Employees and Payments. Below is the contents of the two: The Employees table EmpID |EmpName ------ -------- 113 Harry The Payments table EmpID | Amount | Period | PaymentType |…
0
votes
0 answers

Finding matching sub values?

So not too sure how to go about this, but ideally I want to pull out matching & non-matching info from an array of rows in a table, so I have a table with info like this: 12,2019-07-15 06:02:52,sw72,56,19,DISC,5038-fc:cf:62:88:e9:00 12,2019-07-15…
Alex
  • 1
0
votes
1 answer

Comparing the location of values in different tables

New to SQL. Hope to find distinct pizza sold by restaurants that are located in the same area as bob (customer). Database schema: Customers(cname, area) Restaurants(rname, area) Pizzas(pizza) Sells(rname,pizza,price) Likes(cname, pizza) SELECT…
user144794
0
votes
1 answer

Understanding the given correlated subquery

I am unable to understand how this correlated subquery works on the relation Student={Sid,Sname,marks} Select Sname from Student s1 where (select count (*) from Student s2 where s1.marks<=s2.marks)=3 I cannot understand the way in which I should…
user1369975
  • 217
  • 5
  • 9
0
votes
1 answer

Help writing a query that select the min column in a group by

I have two simple table CREATE TABLE user ( id INT NOT NULL AUTO_INCREMENT, nickname varchar(35) NOT NULL, PRIMARY KEY (id), ); CREATE TABLE user_service ( id INT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, service_type VARCHAR(10)…
Alexis
  • 279
  • 2
  • 6
  • 12
0
votes
3 answers

Select data from single table using Self join

I have requirement to select a data from single table. The requirement is as follows Select data from table which has one value but not another. for e.g I want to select distinct ID which has value 1 but not value 8 present. For for above e.g the…
sagar
  • 3
  • 2
1
2