SQL looping through a table to count and group - sql

I have a table like this
ID|list
1|:A:B:C:
2|:B:D:A:
3|:C:A:D:
I want to count the number of each of A,B,C,D and group them as
A|3
B|2
C|2
D|2
I was thinking about iterating through the table and inserting values in to a temporary table from count statements like the following
select count(*) from table where list like %:A:%
It does look ugly.Is there a better way to do this?
(I am using sqlite from qt)

Never, never, never store multiple values in one column! A better DB design would be
movies table
----------
id
name
release_date
...
actors table
------------
id
first_name
last_name
birth_date
...
movie_cast table
----------------
movie_id
actor_id
You could then select all actors of a specific movie like this
select a.lastName, a.first_name
from actors a
join movie_cast c on c.actor_id = a.id
join movies m on c.movie_id = m.id
where m.name = 'Citizen Kane'
Or select all movies a specific actor stars in like this
select m.name
from actors a
join movie_cast c on c.actor_id = a.id
join movies m on c.movie_id = m.id
where a.lastName ='Wayne'
and a.first_name 'John'

Related

join table on condition

I have 3 tables user, student_data, teacher_data. A user can be either student or a teacher. If it is the teacher I want to join user and teacher_data. And if it is a student then I want to join user with student_data.
How I can do this join with the condition.
I'd combine the two data tables in a sub-query, and then join the users to that.
SELECT
*
FROM
usr u
LEFT JOIN
(
SELECT user_id, datum, xxx, NULL AS yyy FROM student_data
UNION ALL
SELECT user_id, datum, NULL, yyy FROM teacher_data
)
d
ON d.user_id = u.id
https://dbfiddle.uk/?rdbms=oracle_21&fiddle=9b801ea739d42fe50c00ef4e17eaf143
NOTES:
The columns selected from the two data tables must match
Any unmatched columns must either be skipped or filled with NULL
Please don't call a table user, it's a reserved keyword and Oracle won't allow it.
You can write it like this:
select u.user_id,
s.student_id,
t.teacher_id
from usr u
left join student_data s on u.user_id=s.student_id
left join teacher_data t on u.user_id=t.teacher_id
where s.student_id is not null or t.teacher_id is not null
order by u.user_id
For every user_id check if he is a student or teacher, if he is student get his student column values else null, if he is a teacher get his teacher column values else null.
maybe try a union - something like this
select user_id, user_other_stuff
from user, student_data
where user.user_id = student_data.user_id
UNION
select user_id, user_other_stuff
from user, teacher_data
where user.user_id = teacher_data.user_id

SQL add attribute from another table

Table WORK
(WORK ID, TITLE, DESCRIPTION, ARTIST ID)
Table ARTIST
(ARTIST ID, FIRSTNAME, LAST NAME, DATE OF BIRTH, DATE OF DEATH)
Hi
My problem is that I have to display the details of all works of art (including the name of the artist who created the work) that are signed.
I managed to go until
Select * from Work WHERE DESCRIPTION LIKE '%Signed%'
and it displays
WorkID TITLE DESCRIPTION ARTISTID
123 1455 Signed 12
However, I must display
WorkID TITLE DESCRIPTION ARTISTID FIRSTNAME LASTNAME
123 1455 Signed 12
How do I do that? I know it has something to do with JOIN but I'm not very sure.
Inner Join will do or can try left join if u need to
SELECT W.*, A.FirstNAme, A.LastName from work w inner join Artist A on
A.Artistid
= W.ArtistId where A.Description LIKE '%Signed%'
Use inner join to get the records that exist in both tables
Select W.WorkID, W.TITLE, W.DESCRIPTION, W.ARTISTID, A.FIRSTNAME, A.LASTNAME
From Work W
Inner Join Artist A on W.WorkID = A.WorkID
Where Description LIKE '%Signed%'
a left outer join would get all the records in the work table and the matching ones in the artist table.
Check this post out to understand joins a bit more : JOINS

SQL query return name and count of specific attributes across multiple tables

Given multiple tables I'm trying to write a query that returns the names that satisfies a specific count clause.
I have the tables:
genre(genre, movieid)
moviedirectors(movieid, directorid)
directors(directorid, firstname, lastname)
I want to write a query that returns the first and last name of directors that directed at least 50 movies of the genre comedy, and return that number as well.
This is what I have
select d.fname, d.lname, count(*)
from genre g, directors d, moviedirectors md
where g.genre='Comedy' and g.movieid=md.movieid and
md.directorid=d.directorid
group by d.id
having count(*) >= 50
I believe this should be correct but when I run this query on the command line it never finishes. I waited 30 minutes and got no results.
you need inner joins:
SELECT d.fname
d.lname
FROM genre g
INNER JOIN moviedirectors md
ON g.movieid = md.movieid
INNER JOIN directors d
ON md.directorid = d.directorid
WHERE g.genre = 'Comedy'
GROUP BY d.fname, -- group by columns in select
d.lname
HAVING COUNT(*) >= 50
select c.firstname,c.lastname,count(e.movieid) from (select a.* from directors a,movie b where b.genre = 'Comedy' and b.movieid=a.movieid)d,directors c where c.directorid=d.directorid group by e.movieid having count(e.movieid)>50;

SQL Query structure example for recursive lookup

I'm struggling a bit with this query. Perhaps someone can give me a hint?
My setup consists of 3 tables (Actors, Movies, and Relations). What I want to do is:
Find all actors whose name starts with an A that play in any movie alongside Person1.
That's how far i got:
SELECT Name
FROM Actors a, Movies m, Relations r
WHERE a.Name='A%'
AND Person1.keyPerson = m.KeyPerson
AND m.KeyMovies ....
An example on how to continue would be nice!
UPADATE - tables look like this:
Actors (actorsID, name)
Movies (moviesID, title)
Relations (actorsID, moviesID)
There are several ways you can do this - one method you can do is via an IN statement, pulling the movies Person1 has been in, and checking the A% person's movies.
Select Distinct A.Name
From Actors A
Join Relations R On R.ActorId = A.ActorId
Where A.Name Like 'A%'
And R.MovieId In
(
Select R2.MovieId
From Actors A2
Join Relations R2 On R2.ActorId = A2.ActorId
Where A2.Name = 'Person1'
)
Another method is via an EXISTS statement, looking for any movies that Person1 has been in that A% has also been in:
Select A.Name
From Actors A
Join Relations R On R.ActorId = A.ActorId
Where A.Name Like 'A%'
And Exists
(
Select *
From Movies M2
Join Relations R2 On R2.MovieId = M2.MovieId
Join Actors A2 On R2.ActorId = R2.ActorId
Where A2.Name = 'Person1'
And M2.MovieId = R.MovieId
)
A solution without subquery:
SELECT DISTINCT a.Name
FROM Actors a
INNER JOIN Relations r
ON r.actorsID = a.actorsID
INNER JOIN Relations r2
ON r.moviesID = r2.moviesID
INNER JOIN Actors a2
ON a2.actorsID = r2.actorsID
AND a2.Name = 'Person1'
WHERE a.Name LIKE 'A%'

Oracle sql - referencing tables

My school task was to get names from my movie database actors which play in movies with highest ratings
I made it this way and it works :
select name,surname
from actor
where ACTORID in(
select actorid
from actor_movie
where MOVIEID in (
select movieid
from movie
where RATINGID in (
select ratingid
from rating
where PERCENT_CSFD = (
select max(percent_csfd)
from rating
)
)
)
);
the output is :
Gary Oldman
Sigourney Weaver
...but I'd like to also add to this select mentioned movie and its rating. It accessible in inner selects but I don't know how to join it with outer select in which i can work just with rows found in Actor Table.
Thank you for your answers.
You just need to join the tables properly. Afterwards you can simply add the columns you´d like to select. The final select could be looking like this.
select ac.name, ac.surname, -- go on selecting from the different tables
from actor ac
inner join actor_movie amo
on amo.actorid = ac.actorid
inner join movie mo
on amo.movieid = mo.movieid
inner join rating ra
on ra.ratingid = mo.ratingid
where ra.PERCENT_CSFD =
(select max(percent_csfd)
from rating)
A way to get your result with a slightly different method could be something like:
select *
from
(
select name, surname, percent_csfd, row_number() over ( order by percent_csfd desc) as rank
from actor
inner join actor_movie
using (actorId)
inner join movie
using (movieId)
inner join rating
using(ratingId)
(
where rank = 1
This uses row_number to evaluate the "rank" of the movie(s) and then filter for the movie(s) with the highest rating.