need help in SQL Server Query - sql-server-2008

I have a table say Student . This has 2 columns studentid and classid
Anotehr table Class which has ClassID and ClassDescription
Assume , Class has values
ClassID ClassDescription
1 A
2 B
3 C
4 D
5 E
I want to create a query to find out students who have attended A and B . This should be a dynamic query so that next time I require a qry to find out students who have attended A, B and C or B, C and D I should be able to use it.

Create a new table filterClass to be your dinamic source like
FilterID
'A'
'B'
'C'
Then your query is
SELECT S.StudentID
FROM Student S
JOIN Class C
ON S.class_id = C.class_id
JOIN filterClass F
ON C.ClassDescription = F.FilterID
GROUP BY S.StudentID
HAVING COUNT(C.ClassDescription) = (SELECT COUNT(FilterID) FROM filterClass)
In this case SELECT COUNT(FilterID) FROM filterClass will be 3 (A,B,C)

Related

MySQL join table based on condition

I have a requirement where If a certain conditions matches then I need to apply JOIN otherwise, I don't need to apply it.
for example
A,B and C are tables
so I need to join B and C if the B.id not in (select entityId from A) otherwise there will be no join.
also here A and C don't have common values.
Table A
id entityId name
1 xyz abc
2 bcd z
3 edc x
Table B
id entityId shopRegID
1 bcd z
Table C
id entityId newEntityID ShopNewname
1 xyz xyze abcd
2 e ee sd
now I have condition like this, where I need to get the data from these table ,based on the entityId provided to me.
I need to eliminate all the shops which are not registered(not in table B) but include the shops which are renamed(in table C but not in B)
suppose If I am given ('xyz','bcd','edc')
the output should contains
bcd (because it's in table B)
xyz (because it's in table C)
and these table have very large amount of data(in 100 thousands )
what is the best and performant way to achive this.
You can use left join and coalesce as follows:
select *
from tableA A
left join tableB B on a.entityid = b.entityid
left join tableB C on a.entityid = c.entityid
WHERE coalesce(b.entityid,c.entityid) is not null
It sounds like you want entities from a that are in either of the tables. This suggests exists logic:
select a.*
from a
where exists (select 1 from b where b.entityId = a.entityId) or
exists (select 1 from c where c.entityId = a.entityId)

select record only if specific related values are provided in mysql

I 3 have tables, 2(A and B) of them have many to many relationship, they connected through pivot C table. tables desc:
A(id, name)
B(id, is_required)
C(a_id, b_id)
I want to select records from A table, which related record ids from B table are in provided input and fit some condition. for example:
lest say I have list of integers(ids) [1,2,3,4,8,12] and also one record from A has 5 related records from B, example:
A
id name
1 test
-------------
B
id is_required
1 true
2 true
3 false
10 false
16 false
I need to select records from table A join related records from table B, and check - if all required(is_required = true) record ids from B exists in my list ([1,2,3,4,8,12]) then we select this record, otherwise not. so the first example should be selected, because all required records from B (1 and 2) exists in list. for example this:
A
id name
2 test2
-------------
B
id is_required
1 true
2 true
5 true
6 false
should not be selected, because required record with id 5 not provided in list. how can I implement this in mysql? query example:
SELECT A.id, A.name FROM A, B, C
WHERE A.id = C.a_id
AND C.b_id = B.id
as you see, for now its only joins related data, I really don't know how should I implement this. can you please help me?
I believe that you need NOT EXISTS
select A.*
from A
where not exists(
select 1
from C
join B on C.b_id = B.id and
A.id = C.a_id and
is_required = 'false'
)
You can use group by and having:
select c.a_id
from c join
b
on c.b_id = b.id and b.is_required = 'true'
group by c.a_id
having count(*) = (select count(*) from b where b.is_required = 'true');

SQL: Selecting existence of an entry in another table

I am trying to select whether or not an entry exists in another table. Here's a simple example:
Two tables:
Student
ID Major
1 CS
2 CS
3 CS
4 CS
Student_Teacher
SID TID
1 A
1 B
1 C
3 B
3 D
The first table has a list of student IDs (key = Student ID)
The 2nd table has a list of student -> teachers (key = Student ID, Teacher ID combination).
I would like to select ALL students (1,2,3,4; one in each row) and a flag for whether or not they have a teacher.
SELECTED:
ID Flag
1 1
2 0
3 1
4 0
I know this is possible to do using group by:
select Student.ID, count(Student_Teacher.TID)
from Student left join Student_Teacher
group by Student.ID
Is there a simpler way?
You can try joining to a derived table that contains the distinct student id values of the second table:
SELECT ID, IF(ST.SID IS NOT NULL, 1, 0) AS FLAG
FROM Student AS S
LEFT JOIN (
SELECT DISTINCT SID
FROM Student_Teacher
) AS ST ON S.ID = ST.SID

Please help me to write a query for these questions

I have two tables one is student and the other is class:
The first table has the following columns id, marks, classid, studentname
the data of first table is as follow:
id 1 2 3 4
marks 200 250 300 298
classid 2 2 1 3
studentname Vikas Anil Ravil Rahul
The other table has the following columns id, classname
The data of the other table is as follow:
id 1 2 3 4
classname BCA MCA BA BCA
now the question is that
how to get the name of student and class name having the maximum marks.
how to get the name of student and class name having the minimum marks.
how to get the name of the class having maximum students.
Pls Guys help me I'm new with MySQL.
SELECT a.name, b.classname
FROM student a
LEFT JOIN class b
ON a.classid = b.id
WHERE a.marks = (SELECT MAX(marks) FROM student);
SELECT a.name, b.classname
FROM student a LEFT JOIN class b
ON a.classid = b.id
WHERE a.marks = (SELECT MIN(marks) FROM student);
SELECT b.classname
FROM student a
LEFT JOIN class b
ON a.classid = b.id
ORDER BY COUNT(a.classid) DESC LIMIT 1;
not sure but you can try this...im just a newbie too...

Mysql Table Structure Working Fast?

I am planning to create a website similar to IMDB.com. To reduce execution time I am using the following structure. Is it okay for faster working?
Table - 1
Id Movie_name description
1 name one some description
2 name two some description
3 name three some description
Table 2
id actorname
1 name 1
2 name 2
3 name 3
4 name 4
Table 3
id movieid actorid
1 1 1
2 1 2
3 1 3
4 1 9
5 2 6
6 2 5
7 2 8
8 2 1
When I want to list actors in a movie program will retrieve actors ids from table 3 and find respective names from table 2 (using single query). When I want to list the movies of a actor it will retrieve movie ids from table 3 and find respective names from first table. Will it work properly? Any other ideas?
This will give all actors in a specified movie,
SELECT c.ID, c.actorName
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE a.ID = 1
This one will give all movies for a specified actor
SELECT a.*
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID = 1
SQLFiddle Demo (both queries)
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
This is called Relational Division
SELECT a.ID, a.Movie_Name
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID IN (1, 2, 3)
GROUP BY a.ID, a.Movie_Name
HAVING COUNT(DISTINCT c.ID) = 3
SQL of Relational Division
I suggest that you modify table3 by taking away the id field. Use the movieid and actorid together as your primary key. You might want to add other fields to this table such as name of character and order of appearance as suggested in the comment by Jermaine Xu.