SQL - Find names and total budget associated with name - sql

I have a table named PROJECT that looks something like this
SQL> SELECT * FROM PROJECT;
P# PTITLE SPONSOR D# BUDGET
------ ----------------- ----------------------- ---- ----------
1001 Computation Microsoft 1 25000
1002 Study methods Education committee 3 15000
1003 Racing car Cloud Pty Ltd 3 225000
1004 Football Football club 5 35000
1005 Swimming Education committee 5 125000
And from this table, I would like to list the name of sponsors, and the total budget that each sponsor provided.
I am working with Oracle SQL and am having some difficulty with the syntax to select the data from this table.
Thanks in advance!

Simple aggregation should suffice
SELECT SPONSOR,SUM(BUDGET)
FROM PROJECT
GROUP BY SPONSOR

Related

Access SQL to capture all related relatives

I have two Access tables, one regarding persons and the other regarding relatives. The tables relation looks like this.
tbl_Person
PersonID
Name
Age
1001
Abraham
12
1002
Bill
13
1003
Catty
14
1004
Derby
15
tbl_Relative
RelativeID
Name
Age
2001
Zack
42
2002
Yan
43
2003
Xavier
44
tbl_Relation
RelationID
PersonID
RelativeID
Relation
9999
2001
1001
Father
9998
2002
1002
Aunt
9997
2003
1003
Uncle
9996
2001
1004
Father
I would like to select a relative (or person) and have an SQL query find persons (or relatives).
E.g. If I select Yan (2002), the SQL query will discover Bill as the Person related to Yan.
Alternatively if I select Derby (1004), the query will discover Zack as the relative, as well as Abraham, who is also related to Zack.
In essence:
Search : Derby, query finds Zack (father) and Abraham
Search : Yan, query finds only Bill
I can join (union) the two tables to form a combined table, like
Select PersonID, Name, Age, "Person" as category From tbl_Person
UNION
Select RelativeID, Name, Age, "Relative" as category From tbl_Relative

Finding the profit between two tables

Displaying the total profit for each vendor listed in Vendors table
that every product is sold in Products table. Your view column
names should be (name, profit)
Product Table
ID Name Sell cost Quantity V_Id
2008 toy2 25 15 12 1003
2007 toy1 15 5 22 1005
Vendor Table
V_Id Name ZipCode State
1001 James 07101 NJ
1002 Grant 07811 CA
Here's what I have so far but It's not quite working
select Vendors.Name, SUM(sell_Price - cost) as Profit
FROM A_Products, A_Vendors
group by Name
Austin 360
Grant 360
James 360
Sam 360
Wendy 360
Always use explicit join, that way you can avoid ambiguities and more efficient indeed, also use table alias so that it is more readable.
SELECT V.Name, SUM(P.sell_Price - P.cost) as Profit
FROM A_Products P
INNER JOIN A_Vendors V
on P.V_ID=V.V_ID
group by V.Name

Joining Count and select query

Table Genre
GENRE_CATEGORY DESCRIPTION
-------------- ----------------------
C100 Information Technology
C200 Novel
C300 Cookery
C400 Lifestyle
Table Book
ISBN TITLE PAGE_NUMBER PUBLICATION_YEAR PUBLISHER_CODE GENRE_CATEGORY
------------ ----------------- ----------- ---------------- -------------- --------------
123-0-12-374 Rainbow Mountain 200 2011 P2001 C200
989-2-96-545 Data Mining 340 2012 P2002 C100
718-8-16-555 Asian Food 280 2013 P3002 C300
674-9-90-345 Yoga for the mind 180 2016 P2002 C400
900-0-88-767 Finding Rainbow 250 2016 P2001 C200
888-9-55-447 Pastry Heaven 200 2016 P3002 C300
what sql statement do i need to write to produce a table with publisher code, description, Number of books(count number of books using count).
The table must show how many number of books for novel , Information technology,lifestyle and cookery. I've spent two hours trying to figure out what to do please do help me
select Publisher_Code,Description, count(genre_category)
from genre
INNER Join book
ON genre.genre_category=book.genre_category
where(genre.genre_category=book.genre_category);
The output must look like this
I think this is what you are looking for...
select Publisher_Code, Description, count(*)
from genre
INNER Join book
ON genre.genre_category=book.genre_category
GROUP BY Publisher_Code, Description;

SQL SELECT query to pull data from tables to calculate a sum

I have four tables in a database, what I want to do is essentially list the employee number, name department number of any projects they are working on and the total hours that they spend working on projects.
I'm trying to do this all as one single SELECT statement, but am having trouble getting it to work with Oracle SQL. My latest attempt has been
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, SUM(WORKSON.HOURS)
FROM EMPLOYEE JOIN WORKSON ON EMPLOYEE.E#=WORKSON.E#
GROUP BY EMPLOYEE.E#;
Which isn't even the complete statement, and gives me the error:
ERROR at line 1:
ORA-00979: not a GROUP BY expression
What can I do to complete the statement to also include the department number and what should I be doing? Should I be using a join for this? or should I be incorporating another SELECT statement within a WHERE clause?
The tables are as follows:
EMPLOYEE:
SQL> SELECT * FROM EMPLOYEE;
E# NAME DOB S SALARY SUPER D#
----- ------------------ ----------- -- ------ -------- ---
00100 Albert 13-OCT-65 M 186.5
00110 Alvin 13-OCT-77 M 156.4 00100 1
00120 Alice 17-JUN-73 F 156.5 00100 2
00150 Bob 02-JUL-60 M 166.4 00100 3
00200 Carl 02-FEB-67 M 156.3 00100 4
00250 Douglass 14-APR-83 M 156.4 00100 5
00101 Peter 13-NOV-76 M 85.2 00110 1
00103 Ami 12-SEP-85 F 78.2 00110 1
00107 Wendy 12-SEP-88 F 68.2 00110 1
00109 Michael 12-SEP-90 M 58.2 00110 1
00125 Angela 20-NOV-90 F 56.4 00120 2
00105 Robert 15-JAN-86 M 66.2 00150 3
00136 Aban 15-JAN-90 M 55.3 00200 4
00187 Eadger 07-APR-86 M 76.5 00250 5
14 rows selected.
WORKSON:
SQL> SELECT * FROM WORKSON;
E# P# HOURS
----- ------- -----
00110 1001 10
00101 1001 20
00150 1002 10
00105 1002 10
00105 1003 20
00105 1004 20
00250 1004 15
00187 1004 25
00105 1005 15
DEPARTMENT:
SQL> SELECT * FROM DEPARTMENT;
D# DNAME MANAG MSDATE
-- ------------------ ------ ------------
1 SALES 00110 02-JAN-12
2 ACCOUNTING 00120 30-OCT-10
3 GAMES 00150 01-MAR-08
4 HUMAN RESOURCES 00200 02-JAN-13
5 SPORTS 00250 10-MAY-10
PROJECT:
SQL> SELECT * FROM PROJECT;
P# PTITLE SPONSOR D# BUDGET
------ --- -------------- ---------------------- --- -------
1001 Computation Microsoft 1 25000
1002 Study methods Education committee 3 15000
1003 Racing car Cloud Pty Ltd 3 225000
1004 Football Football club 5 35000
1005 Swimming Education committee 5 125000
Any help would be greatly appreciated. Thanks in advance!
You first find the sum of hours of each employee and then join the same with your main table. Inline view can be used for the same.
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, NVL(WORKSON_AGGR.TOTAL_HOURS,0)
FROM EMPLOYEE
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = EMPLOYEE.E#)
Now, you can join your department table too easily.
SELECT
E.E#,
E.NAME,
NVL(WORKSON_AGGR.TOTAL_HOURS,0),
D.DNAME
FROM EMPLOYEE E
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = E.E#)
JOIN DEPARTMENT D ON (D.D# = E.D#)

Using IN operator on a correlated subquery

Consider the following query on the two following Tables:
SELECT sid FROM Salesman SM WHERE sid IN(SELECT sid FROM Sale S WHERE s.sid<>sm.sid)
Sale Table
SALEID SID SLDATE
1001 1 01-Jan-14
1006 1 01-Jun-15
1003 4 01-Feb-14
1002 5 02-Jan-14
1005 2 01-Feb-14
1004 1 01-Mar-14
Salesman Table
SID SNAME LOCATION
1 Peter London
2 Michael Paris
3 John Mumbai
5 Kevin London
4 Harry Chicago
6 Alex Chicago
Why it doesn't fetch any results?
For example: on the first iteration of outer query, inner query should return 2,4,5. But it seems not. Why?
PS: Please ignore the practical use of such a query. This is for academic purpose.
Because it is impossible to fetch anything with this condition, I don't understand what you were trying to do..
As I see it you either meant to select all the sailsmans that didn't make any sales, and in that case this is the right query:
select sid from Salesman SM where sid not in (select sid from Sale S)
or you want only the salesmans that did make a sale, and in that case this is the right query:
select sid from Salesman SM where sid in (select sid from Sale S)