Skip to content
Advertisement

SQLite – Excluding rows from a query based on certain column values, without using correlated subqueries

I’m working with the classicmodels database, originally for MySQL, but which we’re using with SQLite. Within this database, there are 2 tables of interest, the orderdetails table…

… and the products table.

I’ve written a query to list all the distinct order numbers, along with the product lines of the products within their orders. Here is the query, with the first few rows of the corresponding output.

Now, I want to exclude all rows which contain order numbers corresponding to orders which contain Planes. For example, in the output above, order number 10106 contains products that are either Planes or Ships – since 10106 represents an order which contains planes (among other things), BOTH of these rows should be removed in such a query.

A simple subquery approach yields the right answer…

… however the catch here, is that I can’t use correlated subqueries – the question that I am trying to tackle has explicitly stated that these subqueries are not allowed.

What have I tried?

A simple exclusion clause (such as WHERE products.productLine != "Planes") won’t do the trick, as this only removes the order numbers of orders which ONLY contain planes. If an order contains planes and ships (for example), that number will remain in the query – not good!

My initial research into similar questions on StackOverflow seems only to bring up answers suggesting the use of subqueries (which would be awesome in other situations, but unfortunately not in this problem – we’re avoiding subqueries).

Advertisement

Answer

You say “the catch here is that I can’t use correlated subqueries”. But the query that you say yields the right answer, namely…

… does not, in fact, contain a correlated subquery. In fact it doesn’t contain a subquery at all, if you’re going by the true definition of what a subquery is. So if the restriction really is “don’t use subqueries”, then the above query actually abides by the restriction, and is fine.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement