Click here to Skip to main content
15,912,507 members
Home / Discussions / Database
   

Database

 
AnswerRe: MYSQL - right join - where Pin
Mycroft Holmes7-Dec-13 12:22
professionalMycroft Holmes7-Dec-13 12:22 
Questionhow to make remote sql query analyzer in asp.net Pin
raj kumar tamang6-Dec-13 9:19
raj kumar tamang6-Dec-13 9:19 
AnswerRe: how to make remote sql query analyzer in asp.net Pin
Mycroft Holmes6-Dec-13 13:15
professionalMycroft Holmes6-Dec-13 13:15 
Questionms access 2007 update query? Pin
murali_utr5-Dec-13 22:08
murali_utr5-Dec-13 22:08 
AnswerRe: ms access 2007 update query? Pin
Eddy Vluggen5-Dec-13 22:26
professionalEddy Vluggen5-Dec-13 22:26 
AnswerRe: ms access 2007 update query? Pin
Dineshshp5-Dec-13 22:37
professionalDineshshp5-Dec-13 22:37 
GeneralRe: ms access 2007 update query? Pin
Jörgen Andersson5-Dec-13 22:54
professionalJörgen Andersson5-Dec-13 22:54 
QuestionINTERSECT vs SELF-JOINS Pin
Joe Woodbury5-Dec-13 12:16
professionalJoe Woodbury5-Dec-13 12:16 
For the sake of this question, I've massively reduced everything. I know there are alternate solutions, but I want to understand SQL better and solve this specific problem. (So please refrain from suggesting alternate schemas--I already have those; I really want to understand INTERSECT vs SELF INNER JOIN.)

Given two tables with two columns each:

SQL
Table: Words
Id Name
-- ---- 
1  One
2  Two
3  Three

Table: Letters
Id Letter
-- ---- 
1  A
1  B
1  C
2  A
2  D
3  A
3  C


The task is to find all the Ids in Words which match two letters from Letters. For example, I want to find all the rows in Words which match the letters A AND C from letters. Again, massive oversimplification.

I can do the following (in SQLite):

SQL
SELECT DISTINCT Words.Name FROM Words JOIN Letters ON Words.Id=Letters.Id WHERE Letters.Letter='A'
INTERSECT
SELECT DISTINCT Words.Name FROM Words JOIN Letters ON Words.Id=Letters.Id WHERE Letters.Letter='C'
ORDER BY name COLLATE NOCASE


This returns One and Three. I'm happy. Except, it bugs me. I should be able to do the same thing with a series of INNER JOINS, but I've yet to come up with a solution which actually runs. Even with an intermediate table, I'm still not sure how to solve it and thus want to first solve it with this schema. Is there a SQL wizard in the house?

EDIT: Just after posting this, I came up with the following. It works, but is it optimal? Is there a better way? (It seems that if Words is really big, we end up with two huge joins which are then reduced or with the optimizer figure this out?)

SQL
SELECT DISTINCT Words.Name FROM Words 
  INNER JOIN Letters AS L1 ON Words.Id=L1.Id
  INNER JOIN Letters AS L2 ON Words.Id=L2.Id
  WHERE L1.Letter='A' AND L2.Letter='C'
ORDER BY Words.Name COLLATE NOCASE


I aslso got the following suggestion:

SQL
SELECT DISTINCT Words.Name FROM Words 
  INNER JOIN Letters AS L1 ON Words.Id=L1.Id AND L1.Letter='A'
  INNER JOIN Letters AS L2 ON Words.Id=L2.Id AND L2.Letter='C'
ORDER BY Words.Name COLLATE NOCASE

AnswerRe: INTERSECT vs SELF-JOINS Pin
Mycroft Holmes5-Dec-13 13:15
professionalMycroft Holmes5-Dec-13 13:15 
AnswerRe: INTERSECT vs SELF-JOINS Pin
Jörgen Andersson5-Dec-13 21:31
professionalJörgen Andersson5-Dec-13 21:31 
AnswerRe: INTERSECT vs SELF-JOINS Pin
Richard Deeming6-Dec-13 1:43
mveRichard Deeming6-Dec-13 1:43 
QuestionFUNCTION mydatabase.sp_add_new_job_order does not exists Pin
Jassim Rahma5-Dec-13 0:15
Jassim Rahma5-Dec-13 0:15 
QuestionTrouble with date selection Pin
jkirkerx4-Dec-13 11:37
professionaljkirkerx4-Dec-13 11:37 
QuestionThe conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. Pin
Member 104375991-Dec-13 8:07
Member 104375991-Dec-13 8:07 
AnswerRe: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. Pin
RedDk1-Dec-13 9:35
RedDk1-Dec-13 9:35 
AnswerRe: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. Pin
Mycroft Holmes1-Dec-13 11:59
professionalMycroft Holmes1-Dec-13 11:59 
Questionprotect my database - your advsie plz Pin
Jassim Rahma1-Dec-13 5:19
Jassim Rahma1-Dec-13 5:19 
AnswerRe: protect my database - your advsie plz Pin
GuyThiebaut1-Dec-13 23:20
professionalGuyThiebaut1-Dec-13 23:20 
GeneralRe: protect my database - your advsie plz Pin
Jassim Rahma2-Dec-13 22:30
Jassim Rahma2-Dec-13 22:30 
GeneralRe: protect my database - your advsie plz Pin
Richard Deeming3-Dec-13 1:36
mveRichard Deeming3-Dec-13 1:36 
AnswerRe: protect my database - your advsie plz Pin
Eddy Vluggen3-Dec-13 7:12
professionalEddy Vluggen3-Dec-13 7:12 
AnswerWindows Form BindingSource not saving changes in database Pin
Dean Henderson29-Nov-13 10:09
Dean Henderson29-Nov-13 10:09 
QuestionReference Pin
tsunamigang29-Nov-13 1:59
tsunamigang29-Nov-13 1:59 
AnswerRe: Reference Pin
Corporal Agarn29-Nov-13 2:06
professionalCorporal Agarn29-Nov-13 2:06 
GeneralRe: Reference Pin
tsunamigang29-Nov-13 2:12
tsunamigang29-Nov-13 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.