I have 4 tables. One is called artist. Here is the table structure:
artistID lastname firstname nationality dateofbirth datedcease
The other table is called work
workId title copy medium description artist ID
Trans table
TransactionID Date Acquired Acquistionprice datesold
askingprice salesprice customerID workID
Customer table
customerID lastname Firstname street city state
zippostalcode country areacode phonenumber email
First question is which artist has the most works of artsold and how many of the artist works have been sold.
My SQL query is below
SELECT *
FROM dtoohey.artist A1
INNER JOIN
(SELECT
COUNT(W1.ArtistID) AS COUNTER, artistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.work W1 ON W1.workid = T1.Workid
GROUP BY W1.artistID) TEMP1 ON TEMP1.artistID = A1.artistID
WHERE
A1.artistID = TEMP1.artistId
ORDER BY
COUNTER desc;
I am to get the whole table but I want to show only the first row which is the highest count - how do I do that??
qns 2 is sales of which artist's work have resulted in the highest average profit(i.e) the average of the profits made on each sale of worksby an artist), and what is that amount.
My SQL query is below
SELECT
A1.artistid, A1.firstname
FROM
(SELECT
(salesPrice - AcquisitionPrice) as profit,
w1.artistid as ArtistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.WORK W1 ON W1.workid = T1.workid) TEMP1
INNER JOIN
dtoohey.artist A1 ON A1.artistID = TEMP1.artistID
GROUP BY
A1.artistid
HAVING
MAX(PROFIT) = AVG(PROFIT);
I'm not able to execute it
Use limit 1 for your fist question
Your second problem can be solved exactly the same as the first one. Just replace
COUNT(W1.ArtistID)
with
AVG(salesPrice - AcquisitionPrice) as AvgProfit
and then use:
ORDER BY AvgProfit DESC
LIMIT 1
The full query should be:
SELECT A1.artistid, A1.firstname, TEMP1.avgProfit
FROM (SELECT AVG(salesPrice - AcquisitionPrice) as avgProfit, W1.artistid as artistid
FROM dtoohey.trans T1
INNER JOIN dtoohey.WORK W1
ON W1.workid = T1.workid
GROUP BY artistid
ORDER BY avgProfit DESC
LIMIT 1) TEMP1
INNER JOIN dtoohey.artist A1
ON A1.artisid = TEMP1.artistid
Related
I have these tables:
tutors:
tutorid firstname, lastname...
courses:
url tutorid
reviews:
review courseid
I need to select all tutors that have the most reviews. 1 tutor = 1 course.
I first tried to just select courses with the most reviews:
select y.courseid, num from (select courseid,COUNT(reviews.rating) as num
from reviews group by (reviews.courseid)) y;
This selects all urls and the number of reviews for each.
this
select y.courseid, MAX(num) from (select courseid,COUNT(reviews.rating) as num
from reviews group by (reviews.courseid)) y;
would display the single course with most reviews - even if there are other courses with the same (maximum) number of reviews - they won't get displayed.
I'm trying to combat that. I tried
select y.courseid, num from (select courseid,COUNT(reviews.rating) as num
from reviews group by (reviews.courseid)) y
where num = MAX(num);
but get invalid use of group function error.
EDIT: the courseid - is the course's url. As in the course's url is the foreign key in the reviews table.
I would try this one:
select distinct t.tutorid t.firstname, t.lastname
from (select courseid, count(reviews.rating) total
from reviews
group by courseid) r
left join courses c
on r.courseid = c.courseid
left join tutors t
on c.tutorid = t.tutorid
where r.total=(select max(total)
from (select courseid,
count(reviews.rating) total
from reviews
group by courseid) r
)
You can create a column that rank the review in desc order and select those tutor with rank = 1
It would look like this:
Select * from(Select *, rank() over(order by num desc) as rank from table) where rank = 1
You can also use dense_rank base on your need.
tried to display the name of the department that has the least student count.
Try something like this:
SELECT TOP 1 department_name, count(*) AS number_of_students
FROM department natural join student
GROUP BY department_name
ORDER BY number_of_students
It will give you the list of departments ordered by the number of students. By selecting only the first row via TOP 1 you will only get the department with the least number of students.
Is it like this way?
SELECT
T1.department_name, COUNT(T2.department_id) totalCount
FROM department T1
LEFT JOIN student T2
ON T1.department_id = T2.department_id
GROUP BY T2.department_id
HAVING COUNT(T2.department_id) =
(
SELECT
COUNT(T2.department_id) totalCount
FROM department T1
LEFT JOIN student T2
ON T1.department_id = T2.department_id
GROUP BY T2.department_id
ORDER BY totalCount ASC
LIMIT 1
)
SQLFIDDLE DEMO
I have a couple tables in MySQL DB
EID Name
1 Title A
2 Title B
3 Title C
LID EID Location Address Order
1 1 Office NY 1
2 1 Home IL 2
3 2 Office CA 1
4 3 Home NJ 2
I have the above 2 tables (Employee and Location). I would like to know the location of each Employee with office as a preferred choice and if 'office' does not exist then would need 'Home' location . The order column defined the order/priority of what is needed.
here is the output needed
EID LID Name Location Address
1 1 Title A Office NY
2 3 Title B Office CA
3 4 Title C Home NJ
The first join of the query below just connects the Employee and Location tables, but note that it results in all records from Location being joined. The critical part of the below query is the second INNER JOIN to a subquery. This subquery identifies the minimum (i.e. highest priority) order for each employee ID. This is then used to discard records from the first join which are not the highest priority.
SELECT t1.EID,
t2.LID,
t1.Name,
t2.Location,
t2.Address
FROM Employee t1
INNER JOIN Location t2
ON t1.EID = t2.EID
INNER JOIN
(
SELECT EID, MIN(`Order`) AS min_order
FROM Location
GROUP BY EID
) t3
ON t2.EID = t3.EID AND
t2.Order = t3.min_order
One other note: Don't name your columns Order, which is a MySQL keyword. To get my query to work, I had to put it in backticks, which is inconvenient to say the least, and possibly error prone.
Demo here:
SQLFiddle
There are two posibility to get your result.
1)If you need Based on Order result then use this query
SELECT e1.EID, l1.LID, e1.Name, l1.Location, l1.Address
FROM Employee e1
JOIN
(SELECT MIN(`Order `) as Minorder, EID, LID, Location, Address, Order
FROM Location l1
GROUP BY EID) l1
ON l1.EID = e1.EID AND l1.Minorder = l1.Order;
2)if you need result Based on EID then use this query
SELECT e1.EID,l1.LID,e1.Name,l1.Location,l1.Address
FROM Employee e1 JOIN
(SELECT MIN(`EID`)as Mineid,EID,LID,Location,Address,`Order` FROM Location l1 GROUP BY EID)l1
ON l1.Mineid = e1.EID;
Extra Note:-
Plese donot use mysql inbuilt keyword as Column name or Table name for more information read this link click here
You can the expected result by using inner join
Select a.eid,b.Lid,a.name,b.location,b.address from Table1 a innner join (select * from Tableb group by eid) b on
a.eid=b.eid;
you can try this code this will help you as i think
select E.EID,E.name,ad.LID,ad.LOCATION,ad.ADDRESS,ad.[order]
from #emp E inner join #address ad on E.EID = ad.EID
inner join (select EID, min([order]) [order]
from #address
group by EID) tt on ad.EID = tt.EIDand ad.[order] = tt.[order]
I have a table similar to the following:
ID PAYEE CATEGORY
001 Costco Grocery
002 See's Candy
003 Costco Repair
005 Costco Grocery
006 Costco
007 Costco
008 See's
Using MySQL withOUT the aid of a programming language, is there a query (nested or not) that would set the category of the three new rows to the most often used category for those payees?
For example, one of the Costco records (ID 003) has Repair as its category, whereas the other two Costco rows (ID 001 and ID 005) have Grocery as their category. Thus the desired result would be that the new Costco rows (ID 006 and ID 007) would be set to Grocery since that's the most often used category for that payee.
sure.. just change 'your_table' to the name of your table
UPDATE your_table
LEFT JOIN (SELECT payee, category
FROM
(SELECT payee, category FROM your_table WHERE category != '' AND category IS NOT NULL GROUP BY payee, category ORDER BY count(*) DESC) AS tbl2
GROUP BY payee
) AS tbl2 USING (payee)
SET your_table.category = tbl2.category;
this will change the costco that is categorized as repair to 'grocery' as well.. if you dont want this, add:
WHERE your_table.category IS NULL OR your_table.category = ''
to the very end of the query
This would work
UPDATE test t,
(SELECT category,
payee,
count(*)
FROM test ORDER BY count(*) desc LIMIT 1) t1
SET t.category = t1.category
WHERE t.payee = t1.payee
AND (t.category = ''
OR t.category IS NULL)
Sqlfiddle at http://www.sqlfiddle.com/#!2/ed5b0/1/0
I just couldn't figure out a way without repeating the creation of a derived table:
UPDATE t JOIN (
SELECT s1.payee, s1.category FROM (
SELECT payee, category, count(*) cat_count FROM t
WHERE category IS NOT NULL
GROUP BY payee, category
) s1
LEFT JOIN (
SELECT payee, category, count(*) cat_count FROM t
WHERE category IS NOT NULL
GROUP BY payee, category
) s2
ON s1.payee = s2.payee AND s1.cat_count < s2.cat_count
WHERE s2.cat_count IS NULL
) s
ON t.payee = s.payee
SET t.category = s.category
WHERE t.category IS NULL;
Fiddle here
Use mysql's multi-table update:
UPDATE mytable t
JOIN (SELECT payee, category
FROM (SELECT payee, category
FROM mytable
GROUP BY 1, 2
ORDER BY count(*) desc) x
GROUP BY 1) y
ON y.payee = t.payee
SET t.category = y.category
WHERE ifnull(t.category, '') = ''
There's a little bit of kung fu tucked away in there that makes it work: the outer group by returns the first row encountered for the group, which due to the ordering for the inner-most query will be the category with the highest count.
Table Name :: Feedback_master
Fields 1. feed_id 2. roll_id 3. batch_id 4. sem_id (semester ID) 5.f_id (faculty Id) 6. sub_id (subject Id) 7. remark. 8. b_id
Table Name :: subject_master
Fields
sub_id (subject Id)
sub_name (Subject Name0
f_id ( Faculty ID)
Table Name :: faculty_master
Fields
f_id (Faculty Id)
f_name (Faculty Name)
l_name (Faculty Name)
b_id
This are the three tables. Now I want to fetch the detail from this three table.
I want the output as
f_Name (faculty name), Sub_name (Subject Name ) , and remark (Remark ) when i give the (faculty id) f_id
could some one help me to over come this problem.
Using Objects
Select T1.f_name, T2.sub_name, T3.remark from faculty_master as T1,
subject_master as T2, Feedback_master as T3 where T1.f_id = 'your faculty_id'
and T1.f_id = T3.f_id and T2.sub_id = T3.sub_id
heu, MySQL I presume?
SELECT f_name, sub_name, remark
FROM faculty_master
LEFT JOIN subject_master USING(f_id)
LEFT JOIN Feedback_master USING(f_id)
WHERE f_id = the_id_you_want
select fm.f_name, sm.sub_name, remark from faculty_master fm left
join sub_master sm on fm.f_id=sm.f_id
left join feedback_master fbm on
sm.sub_id = fbm.sub_id
where fm.f_id= 123
You can build up the query in stages. The first thing is that you're after a list of feedback remarks, so start with this simple select query:
SELECT * FROM Feedback_master
That's listing all the feedback from all over, but you want to limit it to only feedback on a particular faculty, so let's add a Where clause:
SELECT * FROM Feedback_master
WHERE Feedback_master.f_id = #f_id
Now we've got the right list of records, but the list of fields is wrong. You want the faculty name and subject name, which aren't there in the Feedback_master table; the subject_master and faculty_master tables are linked and assuming that every remark has a subject ID and a faculty ID, we can use a simple inner join to link the tables:
SELECT * FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id
Now it's pulling out all the fields from all three table; this includes all the fields we need, so we can now simply name them in the Select clause:
SELECT
faculty_master.f_name, subject_master.sub_name, Feeback_master.remark
FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id