SQL Join queries - sql

I am beginner in SQL, could you please help me write a query to:
find the name of lecturer who is also a participant in one course
find 4 courses with the most participants
The tables are:
- Lecturer [LecturerID (PK), name]
- Course [CourseId (PK), LecturerID, name]
- Participant [ParticipantID (PK), CourseID(PK)]
Thanks!

If you're trying to learn how joins work, it would be more beneficial for us to help you create the SQL yourself. The basic format for a join is this:
SELECT *
FROM table1
JOIN table2 ON table1.joinID = table2.joinID
I would approach this in 3 steps:
Write a basic SELECT statement that will return the joined table data
Modify the SQL to only show "the name of lecturer who is also a participant in one course"
Starting again with the basic SELECT statement from step 1, modify the SQL to only show "4 courses with the most participants"
These will end up being 2 different queries.
If you want to get a start on it, and get stuck, we can help you along, but it would not help you learn it if we just gave you the SQL. Try writing a little of it, and post what you have when you are stuck.

select L.name from lecture L join participant P on L.id=P.id
select C.cid from course C join participant P
ORDER BY P.cid DESC
LIMIT 4;
Hopefully it helps you

Related

PostgresSQL: Mulltiple table joins, query joined data

I am working on a class question so please dont provide a direct answer but some guidance please.
I have joined 3 tables (businesses, categories, and countries). I need to now answer the following question: "Which are the most common categories for the oldest businesses on each
continent?"
Here is the code that works to join the tables:
SELECT bus.business,
bus.year_founded,
bus.category_code,
bus.country_code,
cat.category,
cou.continent
FROM businesses AS bus
INNER JOIN categories AS cat
ON bus.category_code = cat.category_code
INNER JOIN countries AS cou
ON bus.country_code = cou.country_code
ORDER BY year_founded
I have the 3 tables joined but now need to run an additional query to answer the above question. I have tried writing the COUNT or MIN functions with the initial SELECT for the joins however that throws an error. I also tried using GROUP BY function at the end of the join but also get an error.
Any suggestions would be appreciated

List the names of the course(s) student Altvater took in semester I-2008

Im stuck on a question "List the names of the course(s) student Altvater took in semester I-2008" If anyone can guide me to the right direction I would appreciate it. I assume i would use some sort of join. Please refer to this diagram http://www.csc.villanova.edu/~mdamian/Past/databasefa13/notes/ch07-inclassex.pdf
I understand that I will below parts in my query
Where StudentName = 'Altvater' and
Where Semester = ‘I-2008’
Column names are consistent throughout the schema, allowing natural joins:
SELECT DISTINCT CourseName
FROM STUDENT
NATURAL JOIN REGISTRATION
NATURAL JOIN SECTION
NATURAL JOIN COURSE
WHERE StudentName = 'Altvater'
AND Semester = 'I-2008';
Try following SQL:
SELECT COURSENAME
FROM STUDENT S
INNER JOIN REGISTRATION R
ON S.STUDENTID=R.STUDENTID
INNER JOIN SECTION SE
ON R.SECTOINNO=SE.SECTOINNO
AND R.SEMESTER=SE.SEMESTER
INNER JOIN COURSE C
ON SE.COURSEID=C.COURSEID
WHERE S.STUDENTID=54907/* Altvater */
AND R.SEMESTER='I-2008'
You just need to understand basic design of tables to write SQL. SQL is very easy language. Let me know if you have any doubt in this query.

PostgreSQL. how to make a query that shows pairs from the same column?

I have 3 tables
Students{sId, name}
Class{cId, cName} and
Enrollment{sId, cId, semester}
A student can enroll in multiple classes. I need to produce a query that displays all the pairs of students (their names and ids )who have completed one or more of the same classes in the same semester. I'm not really sure what I am to SELECT to display because I need to display in a format that shows the pairs
I started by doing the joins and came up with this
FROM Students s
INNER JOIN Enrollment e
ON s.sId = e.sId
INNER JOIN Class c
ON c.cId = e.sId
Then I am not sure what to do.. If someone could point me in the right direction it would be much appreciated. I am very new to SQL and am even struggling to find some keywords to google.
Thanks for any help

Multiple Table SQL Statement

I'm struggling with how to structure an SQL statement involving multiple tables and was wondering if anyone could help point me in the right direction.
I assume multiple JOINS will be needed, but I'm not entirely sure of how exactly they work.
Basically the system I need to create will allow teaching staff to view an HTML table of the assignment submissions with relevant details. Below is the ER diagram representing the database.
Each row in the HTML table will relate to a particular assignment submission, and will include the following information - Forename of the student, Surname of the student, Module the assignment relates to, Assignment Name, a link to the feedback PDF document, the time the feedback was collected and the number of times the feedback was commented on.
So basically I need an SQL statement that would return the information mentioned above.
I probably haven't explained this too well, but hopefully the diagram can help fill in the parts I have missed.
SELECT
Forename,
Surname,
ModuleName,
sa.StudentID as StudentID,
sa.AssignmentID as AssignmentID,
FeedbackURL,
FirstViewed,
LastViewed,
ViewCount,
a.Name AS AssignmentName,
FROM assignment AS a
INNER JOIN student_assignment AS sa ON a.AssignmentID=sa.AssignmentID
INNER JOIN student AS s ON sa.StudentID=s.StudentID
INNER JOIN course AS c ON s.CourseID=c.CourseID
INNER JOIN course_module AS cm ON c.CourseID=cm.CourseID
INNER JOIN module AS m ON cm.CRN=m.CRN

Fetching data from two tables matching a criteria

Consider the two tables' schema:
1) Person(name varchar(100),income int)
2) IncomeGroups(incomeGroupName varchar(100), minIncome int, maxIncome int)
I was stumbled while developing a sql query for fetching Person Names with their IncomeGroupNames based on their income.
I am trying to accomplish something like (Name,IncomeGroupName).
Is it even possible? I'll be really glad if anyone can guide me in this.
SELECT a.Name, b.IncomeGroupName
FROM Person a
INNER JOIN IncomeGroups b
ON a.income BETWEEN b.minIncome AND b.maxIncome
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
You can use the following query which joins the tables:
select p.name,
i.incomeGroupName
from person p
inner join incomegroups i
on p.income >= i.minIncome
and p.income <= i.maxIncome;
See SQL Fiddle with Demo
This joins the tables based on the range that the person's income falls in.