www.tutorialspoint.com Forum Index
Register FAQMemberlistUsergroupsTutorials PointLog in
Reply to topic Page 1 of 1
SQL language (WHERE clause)
Author Message
Reply with quote
Post SQL language (WHERE clause) 
Hi Everyone!

I have a Table_1 with columns "Index"and "Name" that looks like this:

Index Name
1 A
1 B
1 C
1 D
-----------
2 A
2 B
2 E
2 G
2 D
-----------
3 D
3 M
-----------
4 A
4 B
4 D
4 M
..........

I need to select from the table all the indices where "Name" contain both A and D. So I need to make query like:

Code:
SELECT Index FROM Table_1
WHERE Name = 'A' AND Name = 'D'

which would give me indices 1, 2, 4.

There is a construction in the language that uses "OR" instead of "AND", but it does not return what I need.

Can anyone help me to construct the query for getting the information I need?

View user's profile Send private message
Reply with quote
Post  
Are these 15 records you mentioned over here or what? I did not understand how you would fetch rows having A and D using AND. In this case you should use OR then it would return you all the rows having A or D.

SELECT Index FROM Table_1
WHERE Name = 'A' OR Name = 'D'


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Reply with quote
Post  
Thank you for the reply Smile
This 'OR' does not work for me, because I need only those indices which contain the both A and D.
I seems the query should be:

Code:
SELECT Indx
FROM Table_1
WHERE name IN('A','D')
GROUP BY Indx
HAVING COUNT(DISTINCT name) >= 2


And it works!

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum