Fetching data from two tables matching a criteria - sql

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.

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

SQL need assistance

Hi all so im stuck on this sql query question: write an sql statement that displays details of session (id and date) that shows crime movies. You must use join to obtain the answer.
I have a table for session with an id column and a date column and i have a movie table that has an moviegenre column with 'crime' under it. I was wondering how i could construct this. I found an example of one:
Select orders.orderid,
customers.customername,
orders.orderdate
from orders
inner join customers
on orders.customerid = customers.customerid;
from this site, but am unsure on how to use this so help answer my question. Also is there a way of doing is problem with using a sub query instead
thank you!
Select s.id,
s.date,
from Session s
inner join Movie m
on m.commoncol = s.commomcol
where m.MovieGenre like 'crime';
commomcol represents the mutual column between the tables. Name of the commoncol can be different in both tables.
Hope it helps!!

Join data on a table based on a relationship table

I have two tables in my database, "Fact" and "Fact_Cause", Here's the table structure:
Fact: ID(PK), Name, Date
Fact_Cause: IDCauseFact(FK -> Fact(ID)), IDEffectFact(FK -> Fact(ID))
What I want is a resultant table with this format
Cause | Effect
Each column containing the Name of the corresponding Fact.
Could you guide me?
Thank you.
Finally worked it out, here's the SQL query:
SELECT cause.Name AS 'Cause', effect.Name AS 'Effect'
FROM Fact_Cause c
INNER JOIN Fact cause ON c.IDCauseFact = cause.ID
INNER JOIN Fact effect ON c.IDEffectCause = effect.Id
I was failing on the joins and the aliases :)

SQL Join queries

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

SQL Counting and Joining

I'm taking a database course this semester, and we're learning SQL. I understand most simple queries, but I'm having some difficulty using the count aggregate function.
I'm supposed to relate an advertisement number to a property number to a branch number so that I can tally up the amount of advertisements by branch number and compute their cost. I set up what I think are two appropriate new views, but I'm clueless as to what to write for the select statement. Am I approaching this the correct way? I have a feeling I'm over complicating this bigtime...
with ad_prop(ad_no, property_no, overseen_by) as
(select a.ad_no, a.property_no, p.overseen_by
from advertisement as a, property as p
where a.property_no = p.property_no)
with prop_branch(property_no, overseen_by, allocated_to) as
(select p.property_no, p.overseen_by, s.allocated_to
from property as p, staff as s
where p.overseen_by = s.staff_no)
select distinct pb.allocated_to as branch_no, count( ??? ) * 100 as ad_cost
from prop_branch as pb, ad_prop as ap
where ap.property_no = pb.property_no
group by branch_no;
Any insight would be greatly appreciated!
You could simplify it like this:
advertisement
- ad_no
- property_no
property
- property_no
- overseen_by
staff
- staff_no
- allocated_to
SELECT s.allocated_to AS branch, COUNT(*) as num_ads, COUNT(*)*100 as ad_cost
FROM advertisement AS a
INNER JOIN property AS p ON a.property_no = p.property_no
INNER JOIN staff AS s ON p.overseen_by = s.staff_no
GROUP BY s.allocated_to;
Update: changed above to match your schema needs
You can condense your WITH clauses into a single statement. Then, the piece I think you are missing is that columns referenced in the column definition have to be aggregated if they aren't included in the GROUP BY clause. So you GROUP BY your distinct column then apply your aggregation and math in your column definitions.
SELECT
s.allocated_to AS branch_no
,COUNT(a.ad_no) AS ad_count
,(ad_count * 100) AS ad_cost
...
GROUP BY s.allocated_to
i can tell you that you are making it way too complicated. It should be a select statement with a couple of joins. You should re-read the chapter on joins or take a look at the following link
http://www.sql-tutorial.net/SQL-JOIN.asp
A join allows you to "combine" the data from two tables based on a common key between the two tables (you can chain more tables together with more joins). Once you have this "joined" table, you can pretend that it is really one table (aliases are used to indicate where that column came from). You understand how aggregates work on a single table right?
I'd prefer not to give you the answer so that you can actually learn :)