Questions tagged [in-subquery]

Use this tag for questions related to subqueries (i.e. a query that is nested inside a SELECT, INSERT, etc.), that use IN at their outer statement.

An example of an is:

WHERE th.parent = 1015 IN (
    SELECT DISTINCT(th1.tid)
    ...
);

as demonstrated in MySQL DELETE FROM with subquery as condition.

Note that this tag is general in its scope, i.e. that it is not restricted in SQL questions only, but any question referring to this type of IN subqueries is welcome to use this tag.

128 questions
1
vote
0 answers

Query with subqueries returning more than I want

I have the following tables (those tables contain many records but for sake of this example I reduced the contents only to the records I want to operate on). Products product_id | product_name ------------+-------------- 1 |…
Jagger
  • 10,350
  • 9
  • 51
  • 93
1
vote
2 answers

Fetch random records which definitely contains a given set of records, using MYSQL

I want to use a query just like this; but I know it's not true usage. In total I need 15 category ids (4,5,6,9,10 and random 10 ids other than the specified) Tried this SELECT * FROM categories WHERE id IN (CONCAT('4,5,6,9,10', SELECT id from…
vural
  • 381
  • 2
  • 11
1
vote
1 answer

same (sub)query multiple time in a mysql query

I have a mysql query like the following. select new,processing,close from (select count(id) new from tickets where id_client in (_a_list_of_client_id) and status = new), (select count(id) processing from tickets where id_client in…
Md Monjur Ul Hasan
  • 1,705
  • 1
  • 13
  • 36
1
vote
1 answer

Tsql IN clause with empty subset

Is it possible to set an IN statement that also matches an empty subset? This is my query: .. WHERE id IN (SELECT * FROM #subset) I would like that record also if #subset is empty. A workaround would be: .. WHERE NOT EXISTS(SELECT * FROM #subset)…
Tobia
  • 9,165
  • 28
  • 114
  • 219
1
vote
2 answers

Is there a workaround to the Oracle Correlated Subquery Nesting Limit?

I have a situation where I'm trying to use a correlated subquery but am running into the nesting limit in Oracle. I might be missing another feature that Oracle has, so I thought I'd post this question here. Does anyone know how to rewrite the below…
copo24
  • 11
  • 2
1
vote
2 answers

MYSQL Inner Join & Get value from Subquery

OK, I am trying to compare two tables and then input a list from a third of names to produce a totals of values for the prior 10 days. The query runs but gives me a NULL result and only one result. If I remove the DATE_ADD and replace it with a…
MykMac
  • 11
  • 2
1
vote
1 answer

MySQL disregards indexes when using IN(SUBQUERY)?

I have the following query. SELECT MIN(col1) FROM table1 WHERE id1 IN (SELECT id2 FROM table2) This produces output that takes ~5 seconds. However, if I change the query to run as two separate queries, such that: SELECT id2 FROM table2 SELECT…
Detritus
  • 357
  • 1
  • 5
  • 13
0
votes
2 answers

Using one cell to reference another database and combine the two in a query

I am trying to figure out if there is a way to take the value in a cell and replace it with the text name of a user that that number refers to in another table. I have looked through subqueries but I don't think that is what I want. So for example…
busboy10
  • 41
  • 4
0
votes
0 answers

MySQL INNER JOIN subquery select one row from second table

I am trying to merge two separate queries that work into one query that would also work. I have several tables and need some data from all of them. I have a table of tasks (this is just the tasks themselves, some may have been completed). Each tasks…
0
votes
1 answer

SQL where clause with optional IDs

Create Proc prcEmployeeSearch( @empIds varchar(200)='' ) As Select empId, empName from tblEmployee Where empId in (select item from dbo.Split(@empIds,',') Go Exec prcEmployeeSearch '' Go Exec prcEmployeeSearch '1,2,5' Go when @empIds is empty I…
0
votes
1 answer

Subquery-error message-Different data types

I run the following SQL code from a public dataset: SELECT station_id,name FROM bigquery-public-data.new_york_citibike.citibike_stations WHERE station_id IN (Select start_station_id from bigquery-public-data.new_york_citibike.citibike_trips where…
Panos
  • 3
  • 1
0
votes
1 answer

Microsoft SQL Server Error: "An expression of non-boolean type specified in a context where a condition is expected, near ','."

I'm trying to execute this query to find the employees with highest salary based on department. Inner query works but I'm getting error from my outer query. I'd highly appreciate it if someone can point me in the right direction. Thank you. Error…
mykeerat
  • 1
  • 1
0
votes
0 answers

Oracle: Joining two select statements containing their own respective joined tables and where clauses

Background: Every order is connected to a load. Each order can only be associated to one load. Loads may be made up of multiple orders. When an load is shipped it creates an attachment for each address associated with the load. The problem: When two…
Theden631
  • 1
  • 2
0
votes
0 answers

Understanding Sub-Queries: Where Exists and IN

When do you use "Where Exists" and when do you use "IN" in a subquery? Is the "where exists" clause associated consider a correlated query? I'm struggling to understand the different types of sub queries and when yo use them. When would I use a sub…
MikeJ
  • 1
  • 1
0
votes
2 answers

Using NOT IN( ... ) when subquery result a single NULL row

I get unexpected results for a NOT IN criteria, when the subquery returns a single NULL result row. There's two tables, brands and media. The goal is to get a result only including the brands that does not have media of the given media_type…
Erik
  • 75
  • 5
1 2 3
8 9