I'm trying to display course number of class by an individual teacher. The course and teacher's id info is on one table and the teacher's id info and name info is on another table. I can get the correct answer for each statement when entered individually but not when combined. so if I have:
select course_no, instructor_id from class;
this gives me the course number and instructor's id
then if i do
select first_name
, last_name
, instructor_id
from instructor
where First_name = 'specific first_name'
and 'specific last_name'
this gives me the instructors id, first & last name but when i join these two i get an error. what I'm i missing
Related
Ques: From the following tables, write a SQL query to find those employees who work in a department where the employee’s first name contains the letter 'T'. Return employee ID, first name and last name.
link to the question https://www.w3resource.com/sql-exercises/sql-subqueries-exercise-14.php
First, one using SELECT
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id IN
( SELECT department_id
FROM employees
WHERE first_name LIKE '%T%' );
Second one without using SELECT
SELECT employee_id, first_name, last_name
FROM employees
WHERE FIRST_NAME LIKE '%t%';
If I'm not mistaken, the exercise wants you to get the requested info regarding ALL employees who are working in the same department with those employees whose fisrt name contains the letter T. So the subquery in your first statement (the correct one) returns all the legitimate department_id for those who have the letter T in their first name, which is then used to get ALL employees working in said department.
Your secondary statement only gets the individual employees whose first name matches a condition, and be done with it , rather than ALL the employees working in the same department with those individual employees.
You will get different results since your second query makes a harder restriction compared to the first.
Assume there is a department where two people are working: Frank and Bert.
The first query will find both of them since they both work in the same department and there is at least one person (Bert) whose name contains the letter T.
Your second query will not find Frank since his name doesn't contain the letter T and you don't care about the department where he and Bert are working.
I have two tables, a NextOfKin table and a Course table, the NextOfKin table has the following attributes:
StudentID
ContactTelNo
And the course has this one (only showing relevant attributes):
CourseNo
I am trying to get an output where I show the studentID and contactTelNo for the next of kin of all students on course with courseNo equal to 1001.
This is the code I'm attempting to run
SELECT studentID, contactTelNo
FROM NextOfKin
WHERE courseNo =
(SELECT courseNo
FROM Course
WHERE courseNo = '1001')
I'm currently getting an error message that says "Unkown coloumn 'courseNo' in 'where clause'
where am I going wrong?
p.s I can only used a nested query and not a join
The subquery needs to return student IDs, not course numbers, since that's what's in the NextOfKin table. And you need to use IN rather than = to check for membership in a set:
SELECT studentID, contactTelNo
FROM NextOfKin
WHERE studentID IN
(SELECT studentID
FROM Course
WHERE courseNo = '1001')
Assuming Course also has a StudentID column, you want this:
SELECT NextOfKin.studentID, NextOfKin.contactTelNo
FROM NextOfKin
INNER JOIN Course ON Course.StudentID = NextOfKin.StudentID
WHERE Course.courseNo ='1001'
If Course does NOT have a StudentID column, you're looking at the wrong table. Somewhere you will have a table with columns for both StudentID and CourseNo values. It might be called something like Enrollments or Registrations.
Assuming only one school can be identified in a specific town, you are required to move students from duplicated schools to originally created schools. We also assume that lowest school id implies the first schools to be created in
the database, hence original schools.
I want to achieve this simply by changing the school ids for duplicate school+town to the smallest school id (the original) in that category. This will take care of the student records table that is linked to this one via foreign key (school id).
How would I go about doing this on the table attached? I'm thinking along the lines of SELECT MIN, CASE STATEMENTS as well as GROUP BY and COUNT() but I'm at a loss on how to combine these. Anyone have an idea/code on how I would achieve the requirement above?
I'd assume that the school id is a unique identifier (key). Therefore you can't just update it in the schools table. You'd rather need to update the school_id column in the students table to point to the original school's id.
If this is the case you can do something along the lines of
-- get all students and their current school info for update
UPDATE students st JOIN schools sc
ON st.school_id = sc.id JOIN (
-- get ids of original schools
SELECT town, name, MIN(id) id
FROM schools
GROUP BY town, name
) q -- join students with a list original schools ids
ON sc.town = q.town AND sc.name = q.name
-- change the school id to the original one
SET st.school_id = q.id
-- but only for students that were associated with non-original schools
WHERE st.school_id <> q.id
Here is dbfiddle demo
I have IT homework that is due at midnight tonight. For our assignment, we had to create a table in an SQL database off my school AFS database. I am using MobaXTerm to do this homework assignment.
I created a table name "student". I created the entire table correctly. It is correct, because my professor gave me the exact command to create it. Here are the columns in order: id, firstname, lastname, address, state, gpa, credits. I populated this table with 20 students, however I do not want to post the picture of the result on here, because it has personal info on it.
I answered the other questions correctly, however I am stuck on this question that has multiple questions in it:
Next, write and run (issue) SQL queries that do the following. For each query, provide screenshots for the SQL query and the results within a Word document so I can grade it.
a. Show state and gpa information about students with the first name Peter (I was told to add students with the name "Peter" before I created this table).
This one is correct here is the command i used:
select state, gpa, firstname from student where lastname = 'Peter';
b. Retrieve the last names, state, and credits of all students that are NOT from AZ or FL. Order by the state.
I am struggling on this one, because I do not know how to show the table of students that are both NOT from AZ and FL.
But here is a command that worked to show if they are not from one state.
select, lastname, credits, state from student where state != 'AZ'
How am I supposed to write that student is not equal to both AZ and FL?
c. How many students live on '10 Main Street'?
select id, address from student where address='10 Main Street';
This question is correct.
d. Retrieve all sophomore student ids along with their credits that are NOT C students (see the table for definition for “sophomore” and “C” grades).
So the table shows that a sophomore has 33-64 credits. A C student has a GPA of 1.7-2.69. So what is my line of command to show these range of numbers?
Q: How am I supposed to write that student is not equal to both AZ and FL?
... WHERE state != 'AZ' AND state != 'FL'
Q: How many students ...
SELECT COUNT(*) AS count_students FROM ...
Q: Sophmore not C
... WHERE credits >= 33 AND credits <= 64
AND NOT ( gpa >= 1.7 AND gpa <= 2.69)
My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name