Sql about find rate is less then 9 and star name - sql

Find every time a student paid 9 or less to see a movie starring Will Smith. Print all the relevant information; order by the name of the student (desc) and then the name of the movie.
there is two table one is call films has student name and amount_paid, movie_name and rate. another table name is movies, has movie_name and star.
Select star, movie_name, amount_paid, rate,
From movies
Where amount_paid < = 9
having star = 'will smith'
Order by student_name desc;
films
STUDENT_NAME MOVIE AMOUNT_PAID RATE
-------------------------- -------------------------- ----------- ----------
Jack Noah 12 5
Jet The Professional 24 10
Lincoln The Weather Man 10 4.5
Smith Despite the Falling Snow 9 3.5
River The Weather Man 2.5 4.5
PHIL FORREST GUMP 18 9
Dave Legend 18 8
movies
MOVIE_NAME STAR
-------------------------- --------------------------
Noah Russell Crowe
Legend will smith

You have an extra comma at the end of line one, and a gap between < and =.
Although trailing commas is okay in JSON, etc, it causes syntax errors in SQL.
And < = instead of <= is likely to break every programming language.
Select star, movie_name, amount_paid, rate -- removed a comma
From movies
Where amount_paid <= 9 -- removed a space
and star = 'will smith'
Order by student_name desc;
EDIT:
As this is from two tables...
SELECT
movies.STAR,
movies.MOVIE_NAME,
films.STUDENT_NAME,
films.AMOUNT_PAID,
films.RATE
FROM
films
INNER JOIN
movies
ON movies.MOVIE_NAME = films.MOVIE
WHERE
films.RATE <= 9
AND movies.STAR = 'will smith'
ORDER BY
films.STUDENT_NAME DESC

this will work:
SELECT
movies.STAR,
movies.MOVIE_NAME,
films.STUDENT_NAME,
films.AMOUNT_PAID,
films.RATE
FROM
films,
movies
where
films.movie=movies.movie_name
and films.AMOUNT_PAID>=9
order by films.STUDENT_NAME,movies.movie_name

Related

More concise way to count no. of lessons/events held if multiple per date exist

This is an example for the question's sake, but I basically have MySQL data similar to the following:
ID
LessonDate
PersonID
Subject
1234
2021-01-11
1
Spanish
1235
2021-01-11
1
Spanish
1236
2021-01-11
2
Spanish
1237
2021-01-12
1
Music
1238
2021-01-12
1
Music
1239
2021-01-12
1
Music
1240
2021-01-12
3
Music
1241
2021-01-12
3
Music
1242
2021-01-13
2
Chemistry
1243
2021-01-13
3
Chemistry
1244
2021-01-13
2
Spanish
1245
2021-01-14
3
Mathematics
This is interpreted to mean that:
Person 1 had two Spanish lessons on 11th Jan (Person 2 attended one of those)
Person 1 had three Music lessons on 12th Jan (Person 3 attended two of those)
Person 2 and Person 3 shared a Chemistry lesson on 13th Jan, while only Person 2 went to Spanish that day, etc.
To get my desired output I'm currently using 4 levels of grouping as follows. Starting with a count per subject-person-date, then the max per subject per date, then the total per subject, and finally listing out the subjects for each total:
SELECT LessonsHeld, GROUP_CONCAT(Subject ORDER BY Subject SEPARATOR ', ') AS Subjects FROM
(SELECT Subject, SUM(DayCount) AS LessonsHeld FROM
(SELECT Subject, LessonDate, MAX(PersonDayCount) AS DayCount FROM
(SELECT Subject, LessonDate, PersonID, COUNT(*) AS PersonDayCount FROM `lessons`
GROUP BY Subject, LessonDate, PersonID) x
GROUP BY Subject, LessonDate) y
GROUP BY Subject) z
GROUP BY LessonsHeld
ORDER BY LessonsHeld DESC
The output:
[LessonsHeld] [Subjects]
3 Music, Spanish
1 Chemistry, Mathematics
Is there a more concise way to count people's multiple events/classes etc. held on given dates? 4 levels of GROUP BY seems a tad extreme here.
Note the reason I have multiple IDs for single events is that each attendee can enter and delete their own data. I've focused on minimising data entry rather than having them do anything extra such as remembering start times or matching up their attendance with each other.
MySql 8.0 supports window functions, so it can be a bit shorter
SELECT LessonsHeld, GROUP_CONCAT(Subject ORDER BY Subject SEPARATOR ', ') AS Subjects
FROM
(SELECT Subject, SUM(DayCount) AS LessonsHeld
FROM
(SELECT Subject, LessonDate,
max(COUNT(*)) over(partition by Subject, LessonDate) AS DayCount,
row_number() over(partition by Subject, LessonDate order by PersonID) rn
FROM `lessons`
GROUP BY Subject, LessonDate, PersonID
) x
where rn = 1
GROUP BY Subject) z
GROUP BY LessonsHeld
ORDER BY LessonsHeld DESC;
Not sure if it will perform better.
MariaDB 10.3 fiddle

Limit column value repeats to top 2

So I have this query:
SELECT
Search.USER_ID,
Search.SEARCH_TERM,
COUNT(*) AS Search.count
FROM Search
GROUP BY 1,2
ORDER BY 3 DESC
Which returns a response that looks like this:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
sally lion 5
john zebra 3
john leopard 1
And my question is: How would I change the query, so that it only returns the top 2 most-searched-for-terms for any given user? So in the example above, the last row for Sally would be dropped, and the last row for John would also be dropped, leaving a total of 6 rows; 2 for each user, like so:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
john zebra 3
In SQL Server, you can put the original query into a CTE, add the ROW_NUMBER() function. Then in the new main query, just add a WHERE clause to limit by the row number. Your query would look something like this:
;WITH OriginalQuery AS
(
SELECT
s.[User_id]
,s.Search_Term
,COUNT(*) AS 'count'
,ROW_NUMBER() OVER (PARTITION BY s.[USER_ID] ORDER BY COUNT(*) DESC) AS rn
FROM Search s
GROUP BY s.[User_id], s.Search_Term
)
SELECT oq.User_id
,oq.Search_Term
,oq.count
FROM OriginalQuery oq
WHERE rn <= 2
ORDER BY oq.count DESC
EDIT: I specified SQL Server as the dbms I used here, but the above should be ANSI-compliant and work in Snowflake.

Is it joins again in ORACLE

Here's the problem:
List the title, authors first and last names and year to date sales for books whose authors have addresses in California and Utah.
the desc tables:
SQL> desc authors
Name
--------------------------
AUTHOR_ID
AUTHOR_LNAME
AUTHOR_FNAME
PHONE
ADDRESS
CITY
STATE
ZIP
SQL> desc title_authors
Name
--------------------------
AUTHOR_ID
TITLE_ID
AUTHOR_ORD
ROYALTY_SHARE
SQL> desc titles
Name
--------------------------
TITLE_ID
TITLE
TYPE
PUBLISHER_ID
PRICE
ADVANCE
YTD_SALES
CONTRACT
NOTES
PUBLICATION_DATE
Here's what Ive tried w/ results......
SQL> SELECT AUTHORS.AUTHOR_FNAME, AUTHOR_LNAME, TITLES.TITLE, TITLES.YTD_SALES
2 FROM AUTHORS
3 JOIN TITLE_AUTHORS ON AUTHORS.AUTHOR_ID = TITLE_AUTHORS.AUTHOR_ID
4 JOIN TITLES ON TITLE_AUTHORS.TITLE_ID = TITLES.TITLE_ID
5 WHERE STATE = 'CA'
6 OR STATE = 'UT';
AUTHOR_FNAME AUTHOR_LNAME TITLE YTD_SALES
-------------------- ---------------------------------------- ---------------------------------------- ----------
Marjorie GreeN The Busy Executive's Database Guide 4095
Marjorie GreeN You Can Combat Computer Stress! 18722
Dick StrAight Straight Talk About Computers 4095
Note 3 full results returned.
Next trial, which Ive tried various versions of, returns 3 full records, but the entire authors list:
AUTHOR_FNAME AUTHOR_LNAME TITLE YTD_SALES
-------------------- ------------ ---------------------------------------- ----------
Marjorie GreeN The Busy Executive's Database Guide 4095
Marjorie GreeN You Can Combat Computer Stress! 18722
Dick StrAight Straight Talk About Computers 4095
Albert Ringer
Ann Dull
JOHNSON White
Chastity Locksley
Anne RINGER
Stearns MacFeatHer
Anne RINGER
Michael O'Leary
Stearns MacFeatHer
Livia Karsen
Abraham BeNNet
Albert Ringer
Michael O'Leary
Sheryl Hunter
Cheryl Carson
Chastity Locksley
What is this? Is it blocked somehow? I can see the entire tables individually.
Any one?
Try this,
SELECT AUTHORS.AUTHOR_FNAME, AUTHOR_LNAME, TITLES.TITLE, TITLES.YTD_SALES
FROM AUTHORS
INNER JOIN TITLE_AUTHORS ON AUTHORS.AUTHOR_ID = TITLE_AUTHORS.AUTHOR_ID
INNER JOIN TITLES ON TITLE_AUTHORS.TITLE_ID = TITLES.TITLE_ID
WHERE STATE = 'CA' OR STATE = 'UT';

Retrieve highest value from sql table

How can retrieve that data:
Name Title Profit
Peter CEO 2
Robert A.D 3
Michael Vice 5
Peter CEO 4
Robert Admin 5
Robert CEO 13
Adrin Promotion 8
Michael Vice 21
Peter CEO 3
Robert Admin 15
to get this:
Peter........4
Robert.......15
Michael......21
Adrin........8
I want to get the highest profit value from each name.
If there are multiple equal names always take the highest value.
select name,max(profit) from table group by name
Since this type of request almost always follows with "now can I include the title?" - here is a query that gets the highest profit for each name but can include all the other columns without grouping or applying arbitrary aggregates to those other columns:
;WITH x AS
(
SELECT Name, Title, Profit, rn = ROW_NUMBER()
OVER (PARTITION BY Name ORDER BY Profit DESC)
FROM dbo.table
)
SELECT Name, Title, Profit
FROM x
WHERE rn = 1;

How to get the student academic progress?

course Table
course_code course_name credit_points_reqd
1 Comp Science 300
2 Soft Engineering 300
subject Table
subject_code subject_name credit_points
CS123 C Prog 15
CS124 COBOL 15
enrolment table
student_id student_name course_code subject_code Results
1 Lara Croft 1 CS123 70
1 Lara Croft 1 CS124 50
2 Tom Raider 2 CS123 60
2 Tom Raider 2 CS124 40
3 James Bond 1 CS123 NULL
3 James Bond 1 CS124 40
OUTPUT TABLE
student_name course_name credit_points_obt credit_points_reqd
Lara Croft Comp Science 30 300
Tom Raider Soft Engineering 15 300
I'm currently using TSQL. So here's the situation. I've prepared these tables to get the output like the way it i showed u up there. I need to calculate the credit points obtained. Credit points are achieved if the student receives > 50 for a subject they took. I want to ignore students that has not received any credit points at all (eg, James Bond is ignored as he has not achieved any points yet)
select student_name, course_name,credit_points_obt,credit_points_reqd
FROM enrolment (SELECT student_full_name, SUM(credit_points) AS credit_points_obt
FROM enrolment
GROUP BY student_id),
Totally stuck...I have no idea where to go now.
You can sum conditionally to get points for subject. If none are given result will be null, so you filter out those student/course pairs in having clause.
I've changed > 50 condition to >= 50 because your results contradict your requirements. Also, by the data I'd say that you have omitted student table for brewity, but if you haven't, it is a must.
Live test is # Sql Fiddle.
select enrolment.student_name,
course.course_name,
course.credit_points_reqd,
sum(case when enrolment.results >= 50
then subject.credit_points
end) credit_points_obt
FROM enrolment
inner join course
on enrolment.course_code = course.course_code
inner join subject
on enrolment.subject_code = subject.subject_code
group by enrolment.student_name,
course.course_name,
course.credit_points_reqd
having sum(case when enrolment.results >= 50
then subject.credit_points
end) is not null