So I'm having an issue with trying to return records that have two conditions which are not equal.
I have three tables: Person, Student, and Instructor. I'm trying to return a list of persons who are neither students or instructors.
Here are the tables (they are all populated with values that fit all criteria):
CREATE TABLE Person (
Name char (20),
ID char (9) not null,
Address char (30),
DOB date,
Primary key (ID)
);
CREATE TABLE Instructor (
InstructorID char (9) not null references Person (ID),
Rank char (12),
Salary integer,
PRIMARY KEY (InstructorID)
);
CREATE TABLE Student (
StudentID char (9) not null REFERENCES Person (ID),
Classification char (10),
GPA DOUBLE,
MentorID char (9) REFERENCES Instructor (InstructorID),
CreditHours integer,
PRIMARY KEY (StudentID)
);
And here is my answer/query:
SELECT Person.ID, Person.Name, Person.DOB
FROM Person
RIGHT JOIN Student ON Person.ID = Student.StudentID
RIGHT JOIN Instructor ON Person.ID = Instructor.InstructorID
WHERE NOT Person.ID = Student.StudentID AND Person.ID =
Instructor.InstructorID;
Did I screw up something on my joins? I'm not getting any errors, but the query isn't returning anything either. I've tried all joins (left, right, full) to no avail.
Any help is greatly appreciated.
You may use NOT IN to determine if the records are not in both of those tables
SELECT id,
name,
dob
FROM Person
WHERE id NOT IN (SELECT studentid
FROM student)
AND id NOT IN (SELECT instructorid
FROM instructor);
You are pretty close. First, use LEFT JOIN so you keep everything in the first table. Second, use comparisons to NULL to see if there are matches:
SELECT p.ID, p.Name, p.DOB
FROM Person p LEFT JOIN
Student s
ON p.ID = s.StudentID LEFT JOIN
Instructor i
ON p.ID = i.InstructorID
WHERE s.StudentID IS NULL AND i.InstructorID IS NULL;
Related
I need to make a query, where there are columns of client's names and their orders per month.
Some clients don't have orders at some months and there fields must have 0.
The problem is, when i use WHERE and OUTER JOIN (no matter which one) at one query*, nessesary zero`s cutteed by WHERE. So how can i solve that?
Descripton of tables are pinned.
SELECT name
, ordering.id_client
, COUNT(order_date)
FROM ordering
RIGHT
OUTER
JOIN client
ON client.id_client = ordering.id_client
WHERE month(order_date) = 1
GROUP
BY name;
**Descripton**: (https://i.imgur.com/TrUGOLW.png)
**Example of my query** (there are 6 notes about clients at my db, showed only 4 of 6):
(https://i.imgur.com/ABP6pP0.png)
**MRE stuff**
Client: create table client(id_client int primary key auto_increment, name var char(50), passport_code int, addr varchar(70));
insert into client values(null, 'Penny Anderson', 6485, 'New Orlean');
Ordering: create table ordering(id_order int primary key auto_increment, id_client int, order_date date, foreign key(id_client) references client(id_client));
insert into ordering values(null, 1, date('2020-05-01'));
Try a simple left join starting from client's table
SELECT client.name
, client.id_client
, COUNT(order_date)
FROM client
LEFT JOIN ordering ON client.id_client = ordering.id_client
AND month(ordering.order_date) = 1
GROUP BY client.id_client;
If the condition for join is related to the left joined table then add this condition in the related ON clause and not in where otherwise this work as an inner join
I need to create a sql that contains a list of users, and for each user the number they have
reviewed.
I tried this, but it didnt give the desired output because i didnt know how to work the SUM into it.
SELECT review.revID, reviewer.name FROM review , reviewer WHERE review.revID = reviewer.revID
Any assistance would be apprectiated
here are my tables
CREATE TABLE reviewer (
revID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(40),
email VARCHAR(40),
password VARCHAR(125)
);
CREATE TABLE movie (
movID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(30),
release_date date
);
CREATE TABLE review (
revID INT,
movID INT,
rating INT CHECK (rating > 0 AND rating < 10) ,
review_date datetime(6),
comment VARCHAR (300),
helpful INT,
PRIMARY KEY (revID,movID),
FOREIGN KEY (revID)
REFERENCES reviewer(revID),
FOREIGN KEY (movID)
REFERENCES movie(movID)
);
Try this query:
SELECT
count(movId) AS Total_Movie_Count,
r.name AS Reviewer_Name,
r.revId as Reviewer_Id
FROM review
INNER JOIN reviewer r on r.revId = review.revId
GROUP BY review.revId
You have to use GROUP BY for aggregated column movId. And to get the reviewer name, you have to INNER JOIN with revId.
Select count(*) as no, revId from review
Group by revId
You can use the above query
Try to use explicit join syntax (e.g. inner join, left join, cross join). Because the relationships among different tables are better explained using explicit join syntax.
SELECT
review.revID,
reviewer.name,
SUM(rating) As totalReview
FROM review
INNER JOIN reviewer ON review.revID = reviewer.revID
GROUP BY reviewer.revID;
Note: You need to use GROUP BY revID in order to get output for each reviewer.
And using aggregate function SUM along with group by will provide total rating / review for each reviewer.
Btw, MYSQL doesn't support CHECK constraint.
create table person {
id int,
name char(10),
primary key(id),
}
create table person_child {
id int,
name char(10),
primary key(id),
foreign key(id) references person(id));
}
Can a person have 1...n child so it is one to many child . my query is to find the name of person with number of child he/she have?
what will be the select statement
Use join and group by
select person.name, count(*) as num_of_child
from person
inner join person_child on person.name = person_child.name
group by person.name;
Select p.name,count(PC.id) from person as p Inner join person_child as PC on p.ID=PC.ID Group by p.name
This would do it
First and foremost, this is part of an assignment, but I'm trying to get more clarification on how the count() function works when querying a db.
The question is: List the name, SSN and the number of courses the student has taken (courses with the same CourseNumber taken in different quarters are counted as different courses).
I've been querying using this:
SELECT S.Name, S.SSN, COUNT(*)
FROM Student S, Enrollment E
WHERE S.SSN = E.SSN
GROUP BY S.SSN
which seems to return the correct answer, but I'm not sure why. As a result, I can't seem to get the next questions (assuming courses with the same CourseNumber taken in different quarters are considered as one course) correct.
Here are the create table commands so you can see which table holds what info:
CREATE TABLE Student(
SSN INT(9),
Name VARCHAR(20),
Major VARCHAR(30),
PRIMARY KEY (SSN)
);
CREATE TABLE Course(
CourseNumber INT(5),
PrerequisiteCourseNumber INT(10),
CourseTitle VARCHAR(10),
NumberUnits INT(2),
PRIMARY KEY (CourseNumber)
);
CREATE TABLE Section(
CourseNumber INT(5),
Quarter VARCHAR(10),
RoomNumber INT(5),
DayTime VARCHAR(20),
PRIMARY KEY (CourseNumber,Quarter),
FOREIGN KEY (CourseNumber) REFERENCES Course(CourseNumber)
);
CREATE TABLE Enrollment(
SSN INT(9),
CourseNumber INT(5),
Quarter VARCHAR(10),
Grade VARCHAR(1),
PRIMARY KEY (SSN,CourseNumber,Quarter),
FOREIGN KEY (SSN) REFERENCES Student(SSN),
FOREIGN KEY (CourseNumber) REFERENCES Course(CourseNumber),
FOREIGN KEY (Quarter) REFERENCES Section(Quarter)
);
Any pointers?
what you are doing is using old join syntax (pre ansi syntax) where you do a cross join of tables and use the where to turn it into a join. an equivalent query would be this.
SELECT S.Name, S.SSN, COUNT(*)
FROM Student S
JOIN Enrollment E ON S.SSN = E.SSN
GROUP BY S.SSN
Now to answer your question about what the count is doing and stuff..
COUNT() returns a count of every row that is returned.
GROUP BY groups all of the records by a common ground aka your SSN field. so if a student has 5 SSN rows in the table then his count will be 5.
Now for the part you are looking for with course numbers.. you need to JOIN those tables on appropriate fields and add a field to your group by for each quarter
aka add to the previous code..
WHERE E.quarter = whatever_quarter_you_want
you can add more to this query if you need to. but add data to your question if you want a more full answer.
Your current query is:-
SELECT S.Name, S.SSN, COUNT(*)
FROM Student S
INNER JOIN Enrollment E
ON S.SSN = E.SSN
GROUP BY S.SSN
What this is doing is joining student to enrollment, so giving multiple rows for each student, one for each course / quarter they are enrolled on. It is then grouped by SSN (student number?) to count up the number of course / quarters they are enrolled on. You should really group by S.Name as well (while MySQL won't object, most flavours of SQL would).
Note that COUNT(*) counts the number of rows. You could use COUNT(E.CourseNumber) which would count the number of rows where course number is not null. Not really useful here, but can be useful with LEFT OUTER JOINs. You can also use COUNT(DISTINCT CourseNumber) to count the number of unique non null course numbers for the student.
A LEFT OUTER JOIN might also be better as this would enable you to return 0 as the counts for students who exist but who are not enrolled in any courses:-
SELECT S.Name, S.SSN, COUNT(DISTINCT E.CourseNumber)
FROM Student S
LEFT OUTER JOIN Enrollment E
ON S.SSN = E.SSN
GROUP BY S.Name, S.SSN
SELECT S.Name, S.SSN, COUNT(*)
FROM Student S, Enrollment E, Section Sec, Course C
WHERE S.SSN = E.SSN AND E.CourseNumber = Sec.CourseNumber
AND Sec.CourseNumber = C.CourseNumber AND Sec.Quarter like E.Quarter
GROUP BY S.Name, S.SSN
Can someone explain me how to query these tables to get number of employees in companies for each country they are present in? Number of resulting columns should be 3, Company, Country and Employees. Company and Branch are connected but Country is connected with City so I am not sure how to solve this. Thanks guys, appreciate it!!!
CREATE TABLE country (
id integer NOT NULL PRIMARY KEY,
name varchar NOT NULL,
population integer check(population > 0)
);
CREATE TABLE city (
id integer NOT NULL PRIMARY KEY,
name varchar NOT NULL,
population integer check(population > 0),
country integer NOT NULL REFERENCES country(id)
);
CREATE TABLE company (
id integer NOT NULL PRIMARY KEY,
name varchar NOT NULL
);
CREATE TABLE branch (
company integer NOT NULL REFERENCES company(id),
city integer NOT NULL REFERENCES city(id),
name varchar,
employees integer NOT NULL,
PRIMARY KEY (company, city)
);
This is what I did but I know it is wrong, I tried my combinations but so far nothing was correct!
SELECT
branch.employees,
company.name AS company,
country.name AS country
FROM branch
INNER JOIN company
ON branch.company = company.id
INNER JOIN country
ON city.country = country.id
ORDER BY country;
SELECT
company.name AS company,
country.name AS country,
sum(branch.employees) AS country_employees
FROM branch
JOIN company ON company.id = branch.company
JOIN city ON city.id = branch.city
JOIN country ON country.id = city.country
GROUP BY company.name, country.name
ORDER BY country.name, company.name;
You want to sum over the number of employees per company, for each of the countries they work in. This means that you have to GROUP BY those two fields while you SUM the number employees per branch. You need to use the city table as well, even though you do not display any data from it.
Read up on aggregate functions for more background information.