S
S
sajgak2011-05-23 20:46:37
SQL
sajgak, 2011-05-23 20:46:37

An interesting (seemingly simple) SQL query?

Just now at the interview, they asked a problem about the following sample:
There are 2 related tables. Table A stores identifiers from table B. It is necessary to select from table B all records whose identifiers are not in table A.
Immediately wrote the following

SELECT * FROM b WHERE b.id NOT IN (SELECT a.idB FROM a)

They said that it is possible faster using JOINs. Now I've been fighting for an hour, all thoughts are left.
Thanks for any hint.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
vinxru, 2011-05-23
@sajgak

select * from b
left outer join a on a.idB = b.id

M
Monca, 2011-05-23
@Monca

Well, what's interesting here?
Find out the answer given a homework assignment?

J
Jazzist, 2011-05-24
@Jazzist

I would argue that with a >80% chance, using really simple rather than compound queries will be more efficient, resulting in:
1. Easier to write
2. Runs faster
3. Thankfully cached
4. Easier to upgrade

C
ComodoHacker, 2011-05-24
@ComodoHacker

It can be third and fourth. And how faster it will depend on the DBMS, version, indexes, volumes and distribution of data, etc.
Perhaps they wanted several options and clever reasoning from you.

M
maratfmu, 2011-05-24
@maratfmu

Look at the execution plan. It can be built in different ways depending on the installed indexes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question