I have table with courseID, studentName and the other table is courseID , courseName user will search for the course name in the student table based on the student name
what sql syntax should I use ? join or inner join
If you use JOIN and specify a constraint (e.g. on a.courseId = b.courseId) it is exactly the same as if you use INNER JOIN, so according to your table structure you should do something like:
select * from studentTable
inner join courseTable
on studentTable.courseID = courseTable.courseID
where studentTable.studentName = 'Jack Black';
In MySQL you could also write
select * from studentTable, courseTable
where studentTable.courseID = courseTable.courseID
and studentName = 'Jack Black';
as it would internally be queried in the same way.
Check out the exact syntax for joins here: http://dev.mysql.com/doc/refman/5.7/en/join.html
You can try this:
select a.studentName,b.courseName
from tbl_name a
inner join tbl_name b
on a.courseID = b.courseID
You join when you are looking for a joined result, such as select all student names along with all their courses. E.g.:
select s.studentname, c.coursename
from course c
join student_takes_course s on s.courseid = c.courseid;
or (as the order of the tables doesn't matter):
select s.studentname, c.coursename
from student_takes_course s
join course c on s.courseid = c.courseid;
JOIN is just short for INNER JOIN. You can use either.
But when you are not interested in the joined result, then it's a good habit not to join. Such as when you simply want to show the course names from the course table, as in your example. You would then use IN or EXISTS usually:
select coursename
from course
where courseid in
(
select courseid
from student_takes_course
where studentname = 'Joe'
);
or
select c.coursename
from course c
where exists
(
select *
from student_takes_course s
where s.studentname = 'Joe'
and s.courseid = c.courseid
);
(I prefer IN clauses over EXISTS clauses for their simplicity and use them when possible.)
Related
I want to relate two tables and a relation table. Tables are: Person with it's primary key named id_person, Activity with it's primary key named id_activity and a table that relates the previous two tables: Activity_Person that contain as primary and foreign keys id_activity and id_person.
To relate this tables using the old JOIN format this would work:
select * from activity, person, activity_person
where activity.id_activity = activity_person.id_activity and person.id_person = activity_person.id_person;
This would show the activities that each person has taken part on.
But now I'm learning about JOINs and I don't know what's the correct format to relate a table that appears twice (Activity_Person).
I have tried this:
select * from
person inner join activity_person on person.id_person = activity_person.id_person,
activity inner join activity_person on activity.id_activity = activity_person.id_activity;
But I get the following error:
Not unique table/alias: 'activity_person'
What's the correct format?
You don't need activity_person twice here. Just do
select *
from person
inner join activity_person on person.id_person = activity_person.id_person
inner join activity on activity.id_activity = activity_person.id_activity;
I think you just want two joins:
select *
from person p inner join
activity_person ap
on p.id_person = ap.id_person inner join
activity a
on a.id_activity = ap.id_activity;
I'm not sure why you are trying to repeat activity_person in your query.
Also note that table aliases make the query easier to write and to read.
Your syntax is incorrect.
select * from activity, person, activity_person
where activity.id_activity = activity_person.id_activity
and person.id_person = activity_person.id_person;
Is equivalent to :
select *
from person
inner join activity_person
on person.id_person = activity_person.id_person -- <- remove the comma there
inner join activity
on activity.id_activity = activity_person.id_activity;
Basically, the syntax is like this :
SELECT <the fields to select>
FROM <table name>
JOIN <table to join>
ON <joining condition>
-- if you want to add another table :
JOIN <new table to join>
ON <joining condition>
I am working on the first problem of the famous SQLzoos and am working on the using Null section: http://sqlzoo.net/wiki/Using_Null
The question is:
List the teachers who have NULL for their department.
The corresponding SQL query would be:
SELECT t.name
FROM teacher t
WHERE t.dept IS NULL
Is this a type of anti-join? Specifically, is this a left-anti-join?
This isn't a join at all.
The statement is filtering only records for teachers who don't have an assigned department.
Set Difference
The set difference of teachers and departments, teacher \ department would be a kind of "anti-join"
SELECT
t.name
FROM teacher t
LEFT JOIN department d ON d.id = t.dept_id
WHERE d.id IS NULL
At first glance, this statement does what your statement does, if the foreign key reference was enforced, it would guarantee to do exactly that. However, one use for this statement would be to retrieve teachers who are assigned to departments that have since been deleted (e.g. if the English Lit Dept. & English as 2nd Lang Dept. were reorganized as the English Dept.)
Symmetric Difference
Another "anti-join" would be the symmetric difference, which selects elements from both sets ONLY if they cannot be joined, i.e
(teacher \ department) U (department \ teacher)
I can't think of a motivating example using teachers and departments, but one way to write the symmetric difference on databases that support the FULL OUTER JOIN would be:
SELECT
t.name
FROM teacher t
FULL OUTER JOIN department d ON d.id = t.dept_id
WHERE d.id IS NULL OR t.id IS NULL
For MySQL, this statement would have to be written as the union of two statements.
SELECT
t.name teacher_name, d.name department_name
FROM teacher t
LEFT JOIN department d ON d.id = t.dept_id
WHERE d.id IS NULL
UNION ALL
SELECT
t.name teacher_name, d.name department_name
FROM teacher t
LEFT JOIN department d ON d.id = t.dept_id
WHERE t.id IS NULL
Looking through one of my projects, I found this one use of symmetric difference:
Context:
I have three tables: users, users_gameplay_summary, users_transactions_summary. I needed to email those users who created their accounts in the past 7 days AND one of the following
have transacted but have not played or played but have not transacted.
To get the list, I have this query (note, this was written for Postgresql, and won't work on MySQL, but it illustrates the symmetric difference use case):
SELECT
COALESCE(g.user_id, t.user_id) user_id
FROM users_gameplay_summary g
FULL OUTER JOIN users_transactions_summary t ON t.user_id = g.user_id
WHERE COALESCE(g.user_id, t.user_id) IN (
SELECT user_id
FROM users
WHERE created_at > CURRENT_DATE - '7 day'::interval)
AND (g.user_id IS NULL OR t.user_id IS NULL)
Not exactly, your not actually joining anything now,
in the case of a left anti join you would have access to the department name as well. (although it would be NULL)
Your sql code would be a correct answer for the question you gave though.
A left anti join would be:
SELECT t.name
FROM teacher t
LEFT JOIN dept d ON d.id = t.dept
WHERE d.id IS NULL
To solve this problem of listing teachers without assigned departments, you don't need a JOIN between teacher and dept tables.
dept table is basically a dictionary table that you join to, to translate ids to corresponding names.
teacher table has a dept column which normally could have a FOREIGN KEY constraint to id column in dept table.
Your query is not an ANTI-JOIN. This is a simple projection and selection query using one table.
SELECT t.name
FROM teacher t
WHERE t.dept IS NULL
For an ANTI-JOIN you would at least need a JOIN operation between more than one table at first.
Normally an ANTI-JOIN could look like:
Using LEFT JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.join_column = t2.join_column
WHERE t2.join_column IS NULL
Using NOT EXISTS
SELECT *
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.join_column = t2.join_column
)
I have the next tables: faculty, faculty_subjects and subjects. Every faculty has a list of subjects
How can I SELECT subjects that are not needed for this faculty ?
Note: if user adds some new subject, then this subject doesn't already have a record in faculty_subjects table, but it should be displayed in SELECT.
So to sum up - from the list of ALL subjects that exists I want to know the ones that doesn't needed for this faculty. How this can be achieved ?
I'm using MySQL database.
PS. if there is a more better title - please edit.
You can LEFT JOIN to the Faculty_Subject, with the particulary Faculty ID you are interested in used in the join predicate, then filter out the rows with a match using the WHERE predicate, so you are left with the subjects you want:
SELECT s.id, s.Name
FROM Subject AS s
LEFT JOIN Faculty_Subject AS fs
ON fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
WHERE fs.id IS NULL;
Note, I would usually advise to use NOT EXISTS syntax:
SELECT s.id, s.Name
FROM Subject AS s
WHERE NOT EXISTS
( SELECT 1
FROM Faculty_Subject AS fs
WHERE fs.Subject_idSubject = s.ID
AND fs.Faculty_idFaculty = 1
);
Which I beleive is more logical to the reader, but in MySQL LEFT JOIN/IS NULL performs better than NOT EXISTS
SELECT name FROM Subject WHERE id NOT IN
(SELECT FS.Subject_idSubject
FROM Faculty_Subjects AS FS
INNER JOIN Faculty AS F ON F.id=FS.Faculty_idFaculty
WHERE F.name='Physics' AND FS.Subject_idSubject IS NOT NULL)
I want to get numOfItem from table BUY using ticketTypeId and then using the BUY.userId to find in the table USER to get the gender. So I can get numOfItem from table BUY and gender from table USER. I don't know how to write this in one query. Any idea?
table structure:
TABLE BUY:
ticketTypeId
numOfItem
userId
TABLE USER:
gender
You need to join your tables on a common field, in this case user id
Select b.ticketTypeId, b.numOfItem, b.userId, u.gender
From buy b inner join user u on b.userid = u.userid
Where b.ticketTypeId = <val>
You want to include where to get only needed ticketTypeId
Generally speaking a join between two tables is something like:
select table1.*,table2.*
from
table1
join table2 on table1.key=table2.key
Add userId in the table user
join the tables with inner join in the select statement
select a.*,b.* from [user] a inner join [buy] b on a.userid = b.userid
You need to use a join. Here is an link
SELECT tb1.ticketId, tb1.numOfItem, tb1.userId, tb2.gender
FROM Table1 as tb1
JOIN Table2 as tb2
ON tb1.userId = tb2.userId
Table 1 :- tbl_contacts
Fields
user_id
contact_id
first_name
last_name
Table 2 :- tbl_phone_details
Fields
contact_id
phone_number
phone_type
Table 3 :- tbl_email_details
Fields
contact_id
email_address
email_type
QUERY -
SELECT
tbl_contacts.*, tbl_email_details.*, tbl_phone_details.*
FROM
tbl_contacts, tbl_email_details,
tbl_phone_details
WHERE
tbl_contacts.user_id = '1'
I want to get first_name, last_name, Phone and Email details of particular user_id. I have used above query but its giving me repeated results and I am having less knowledge on DB queries like JOIN and INNER QUERY.
If anyone has any idea, please kindly help.
OUTPUT NEEDED:-
contact_id, first_name, last_name, phone_number, phone_type, email_address, email_type
(Here email and phone number can have 1 or more values for particular users).
Try like this
If you want to retrieve data for particular ID
SELECT T.contact_id,
T.first_name,
T.last_name,
P.phone_number,
P.phone_type,
E.email_address,
E.email_type
FROM tbl_contacts T LEFT JOIN tbl_phone_details P ON
T.contact_id = P.contact_id
LEFT JOIN tbl_email_details E ON
T.contact_id = E.contact_id
WHERE T.contact_id = #contact_id
If you want to retrieve all data
SELECT T.contact_id,
T.first_name,
T.last_name,
P.phone_number,
P.phone_type,
E.email_address,
E.email_type
FROM tbl_contacts T LEFT JOIN tbl_phone_details P ON
T.contact_id = P.contact_id
LEFT JOIN tbl_email_details E ON
T.contact_id = E.contact_id
SELECT tbl_contacts.*, tbl_email_details.*, tbl_phone_details.* FROM
tbl_contacts, tbl_email_details, tbl_phone_details WHERE
tbl_contacts.user_id = '1'
You forgot to mention the condition by which you are going to join all the tables!
SELECT c.first_name, c.last_name, p.phone_number, e.email_address
FROM tbl_contacts c, tbl_email_details e, tbl_phone_details p
WHERE tbl_contacts.user_id = '1'
AND c.contact_id = e.contact_id
AND e.contact_id = p.contact_id;
SELECT c.contact_id, c.first_name, c.last_name,
phone.phone_number, phone.phone_type,
email.email_address, email.email_type
FROM tbl_contacts c
LEFT JOIN tbl_email_details email ON c.contact_id = email.contact_id
LEFT JOIN tbl_phone_details phone ON c.contact_id = phone.contact_id
WHERE tbl_contacts.user_id = '1'
Sql queries are easy to learn and write and they are very useful in getting the important data from database. Joins are used to basically fetch the data from two or more tables on the bases of common column in both tables.
Inner Join will select those values that are common in both of the tables.
Left Join will select all the data from the left table and Right Join will select all the data from right table on the basis of id.
These are the basics of SQL and you must know how to fetch accurate data using them.
this is how I would make that request with JOIN ... but there might be some better or faster way to do it.
SELECT first_name, last_name, phone_number, email_address
FROM tbl_contacts
JOIN tbl_phone_details
ON tbl_contacts.contact_id=tbl_phone_details.contact_id
JOIN tbl_email_details
ON tbl_email_details.contact_id=tbl_contacts.contact_id
WHERE tbl_contacts.user_id = '1';
And just so you don't get lost in all the different answers here (which are probably all correct):
you don't need to give an aliase name to your tables (it's just for readability)
you don't need to mention the table names in your column list if the column name is unique (e.g first_name is only in tbl_contacts). Just if you want the contact_id then you should decide which one (e.g. tbl_phone_details.contact_id)
the multi-select as Jayaram proposed, is exactly the same as the JOIN. MySQL handles both queries the same way (I just didn't see his answer when I responded, sorry)