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;
Related
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
I am trying to write a query for this question.
Find the IDs of all students who were taught by an instructor named
Einstein; make sure there are no duplicates in the result.
One suitable answer to this would be
select distinct student.ID
from (student join takes using(ID))
join (instructor join teaches using(ID))
using (course_id, sec_id, semester, year)
where instructor.name = 'Einstein'
but I don't want to use the join.....using Syntax. I want to write the same query without using join....using. I was able to write some parts of the query, but don't understand how to write the whole query without it returning any errors. Below is what I am trying to do, As an alternative to the join....using syntax the query I am trying to write is by enumerating relations in the from clause, and adding the corresponding join predicates on ID, course id, section id, semester, and year to the where clause. But when I do that, its giving me back errors, saying that "no such column exists"
select distinct student.id
from student, takes
where student.id = takes.id
and student.sec_id = takes.sec_id
and student.semester = takes.semester
and student.year = takes.year
How can I fix the code?
Table setup:
Student
ID NAME DEPT_NAME TOT_CRED
-------------------------------------
00128 Zhang Comp. Sci. 102
12345 Shankar Comp. Sci. 32
19991 Brandt History 80
44553 Peltier Physics 56
Instructor
ID NAME DEPT_NAME SALARY
---------------------------------------
10101 Srinivasan Comp. Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
Teaches
ID COURSE_ID SEC_ID SEMESTER YEAR
------------------------------------------
10101 CS-101 1 Fall 2009
10101 CS-315 1 Spring 2010
10101 CS-347 1 Fall 2009
22222 PHY-101 1 Fall 2017
Takes
ID COURSE_ID SEC_ID SEMESTER YEAR GRADE
------------------------------------------------
00128 CS-101 1 Fall 2009 A
00128 CS-347 1 Fall 2009 A-
12345 CS-101 1 Fall 2009 C
44553 PHY-101 1 Fall 2017 B-
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
Question brief
I'm doing this practice on w3resource and I couldn't understand why the solution worked. I'm 2 days old to SQL. I'll appreciate very much if someone can help me explain.
I have 2 tables, COMPANY(com_id, com_name) and PRODUCT(pro_name, pro_price, com_id). Each company has several products with different prices. Now I need to write a query to display companies' name together with their most expensive products respectively.
The sample answer on the practice is like this
SELECT c.com_name, p.pro_name, p.pro_price
FROM product p
INNER JOIN company c ON p.com_id = c.com_id
AND p.pro_price =
( SELECT MAX(p.pro_price)
FROM product p
WHERE p.com_id = c.com_id );
The query returned expected result.
com_name pro_name pro_price
--------- --------- -----------
Samsung Monitor 5000.00
iBall DVD drive 900.00
Epsion Printer 2600.00
Zebronics ZIP drive 250.00
Asus Mother Board 3200.00
Frontech Speaker 550.00
But I cannot understand how, especially the part inside the bottom sub-query. Isn't SELECT MAX(p.pro_price) supposed to return only 1 highest price of all companies together?
I also tried subsecting this sub-query like this
SELECT MAX(p.pro_price)
FROM product p
INNER JOIN company c ON p.com_id = c.com_id
WHERE p.com_id = c.com_id;
... and it only returned 1 maximum value.
max(p.pro_price)
-----
5000.00
So how does the final result of the whole query include more than 1 records? There's no GROUP BY or anything.
By the way, the query seemed to use 2 conditions for INNER JOIN. But I also tried swapping the 2nd condition into a WHERE clause and it still worked the same. This is one more thing I don't understand.
The databases involved
COMPANY table
COM_ID | COM_NAME
----------------
11 | Samsung
12 | iBall
13 | Epsion
14 | Zebronics
15 | Asus
16 | Frontech
PRODUCT table
PRO_NAME PRO_PRICE COM_ID
-------------------- ---------- ---------
Mother Board 3200 15
Key Board 450 16
ZIP drive 250 14
Speaker 550 16
Monitor 5000 11
DVD drive 900 12
CD drive 800 12
Printer 2600 13
Refill cartridge 350 13
Mouse 250 12
The sub-query is a correlated sub-query. This query is executed for each value of c.com_id in the outer query:
WHERE p.com_id = c.com_id
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