I need to get the data of all teachers who gives a course but does not have an assignement. I tried using inner or left join so if the name of the teacher is not in the table of the assignments it will show the data. If the name of the teacher is in the table of assignments it must not show the data. But I could not get it working.
What am I doing wrong?
select teachers.nameteacher, courses.namecourse, courses.codecourse
from teachers
inner join courses
on teachers.codecourse = courses.codecourse
left join assignments
on assignments.nameteacher = teachers.nameteacher
where teachers.nameteacher IS NULL
filter should be from assignments table
SELECT t.nameteacher,
c.namecourse,
c.codecourse
FROM teachers t
INNER JOIN courses c
ON t.codecourse = c.codecourse
LEFT JOIN assignments a
ON a.nameteacher = t.nameteacher
WHERE a.nameteacher IS NULL --should be assignments
Note : start using alias names to make the query more readable
The easy (and efficient) way, if you are using PostgreSQL or Microsoft SQL Server is to say:
select
a.nameteacher,
c.namecourse,
c.codecourse
from
(
select nameteacher from teachers
except
select nameteacher from assignments
) a
join teachers b
on a.nameteacher = b.nameteacher
join courses c
on b.codecourse = c.codecourse;
If you are using Oracle change "except" to "minus".
If you are using SQLite or MySQL you need to use one of the other suggestions because these don't support except or minus.
Related
I'm use this SQL query to fetch data from a access database
SELECT Absence.date_, stages.name, course.course_name, student.name AS st_name
FROM (((Absence INNER JOIN course ON Absence.course_id = course.ID)
INNER JOIN stages ON course.id_stage = stages.ID)
INNER JOIN student ON Absence.student_id = student.ID AND stages.ID = student.stage_id`
when i wont to group the field or get count of field it show to me this error message
Tried to execute a query that does not include the specified
expression 'Absence.date_' as part of an aggregate function
how can i get number of row ,distinct it by st_name , and group by stage name or course name
For getting the count you should use
SELECT Absence.date_, stages.name, course.course_name, student.name AS st_name, count(*)
FROM (((Absence INNER JOIN course ON Absence.course_id = course.ID)
INNER JOIN stages ON course.id_stage = stages.ID)
INNER JOIN student ON Absence.student_id = student.ID AND stages.ID = student.stage_id
GROUP BY Absence.date_, stages.name, course.course_name, student.name
What table contains the field "Absence.date_" ?
And you do not need to specify "Inner" (or Natural for that matter).
When using aggregate functions (Count(), Sum(), ect) your group by must contain the same list of fields as your select statement. And that error states that either Absence.date_ does not exist as a field in any of the tables in the from clause. But, you have no Group By statement in your query.
I have four tables :
Applicant (aid, aname)
entrance_test (Etid, etname)
etest_centre (etcid, location)
etest_details (aid, etid, etcid, etest_dt)
I want to select the number of applicants who have appeared for each test, test center wise.
This is my current query:
select
location, etname, count(Aid) as number of applicants
from
applicant as a
inner join
etest_details as d on a.aid = d.aid
inner join
Entrance_Test as t on t.Etid = d.Etid
inner join
Etest_Centre as c on c.Etcid = d.Etcid
group by
Location, Etname
This is the error I am getting :
Ambiguous column name 'Aid'
You have the column aid in multiple tables, and it doesn't know which to pick from. You should specify which table it is from using the aliases you defined.
In this case, since a.Aid is the same as d.Aid (due to the JOIN), I'm using the a alias, but do keep in mind if location and etname also appear in multiple tables, you need to specify which table it should pick from.
Select c.location, t.etname, Count(a.Aid)
From Applicant As a
Inner Join etest_details As d On a.aid = d.aid
Inner Join Entrance_Test As t On t.Etid = d.Etid
Inner Join Etest_Centre As c On c.Etcid = d.Etcid
Group By c.Location, t.Etname
As a rule of thumb, when you have multiple sources in one query, you should always be explicit about which table it should come from. Even if you're sure it only exists in one of them, it's a good habit to get into to avoid issues like this in the future.
You need to mention the alias in the COUNT clause. Since you are using aliases, it would be better if you use them in the SELECT and GROUP BY sections as well. In this case, it should be :
SELECT a.location,
a.etname,
COUNT(d.Aid)
FROM applicant AS a
INNER JOIN etest_details AS d ON a.aid = d.aid
INNER JOIN Entrance_Test AS t ON t.Etid = d.Etid
INNER JOIN Etest_Centre AS c ON c.Etcid = d.Etcid
GROUP BY a.Location,
a.Etname
I have create many to many relationships using code first entity framework.
Here the tables generated in DB:
I want to make sql query inner join Students on the Courses,but I don't know how to implement it in this case when bridge table between them.
How can I make sql query inner join Students on the Courses?
Try this
Select c.CourseId,c.CourseName,sc.StudentId,s.StudentName
from Courses c Inner Join StudentCourses sc On c.CourseId = sc.Course_CourseId
Inner Join Student s ON sc.Student_StudentId = s.StudentId
I have a table containing lessons that I called "cours" (french) and I have several cours inside and I have linked them to students with a table between them to see if they go to the lessons or not.
I would like to return data with the SELECT and the data that are NOT select.
So, If one student follow 3 courses of 5, I would like to return the 3 courses that he follow and the 2 courses that he doesn't follow.
Is there a way to do it ?
This will vary based on your RBDMS, but something similar to the following should work:
SELECT
s.Name,
l.Name,
CASE WHEN sl.StudentID IS NULL
THEN 'Not Follows'
ELSE 'Follows'
END AS Status
FROM
Student s
CROSS JOIN Lessons l
LEFT JOIN Student_Lessons sl
ON s.ID = sl.StudentID
AND l.ID = sl.LessonID
this is an outer join. if you identify your database engine (which may have specific outer join syntax) and your schema, then you can get some help with the actual SQL.
I have a statement like this:
select lastname,firstname,email,floorid
from employee
where locationid=1
and (statusid=1 or statusid=3)
order by floorid,lastname,firstname,email
The problem is the column floorid. The result of this query is showing the id of the floors.
There is this table called floor (has like 30 rows), which has columns id and floornumber. The floorid (in above statement) values match the id of the table floor.
I want the above query to switch the floorid values into the associated values of the floornumber column in the floor table.
Can anyone show me how to do this please?
I am using Microsoft sql server 2008 r2.
I am new to sql and I need a clear and understandable method if possible.
select lastname,
firstname,
email,
floor.floornumber
from employee
inner join floor on floor.id = employee.floorid
where locationid = 1
and (statusid = 1 or statusid = 3)
order by floorid, lastname, firstname, email
You have to do a simple join where you check, if the floorid matches the id of your floor table. Then you use the floornumber of the table floor.
select a.lastname,a.firstname,a.email,b.floornumber
from employee a
join floor b on a.floorid = b.id
where a.locationid=1 and (a.statusid=1 or a.statusid=3)
order by a.floorid,a.lastname,a.firstname,a.email
You need to use a join.
This will join the two tables on a certain field.
This way you can SELECTcolumns from more than one table at the time.
When you join two tables you have to specify on which column you want to join them.
In your example, you'd have to do this:
from employee join floor on employee.floorid = floor.id
Since you are new to SQL you must know a few things. With the other enaswers you have on this question, people use aliases instead of repeating the table name.
from employee a join floor b
means that from now on the table employee will be known as a and the table floor as b. This is really usefull when you have a lot of joins to do.
Now let's say both table have a column name. In your select you have to say from which table you want to pick the column name. If you only write this
SELECT name from Employee a join floor b on a.id = b.id
the compiler won't understand from which table you want to get the column name. You would have to specify it like this :
SELECT Employee.name from Employee a join floor b on a.id = b.id or if you prefer with aliases :
SELECT a.name from Employee a join floor b on a.id = b.id
Finally there are many type of joins.
Inner join ( what you are using because simply typing Join will refer to an inner join.
Left outer join
Right outer join
Self join
...
To should refer to this article about joins to know how to use them correctly.
Hope this helps.