Small hint for making query in Mariadb - mariadb

how can i make a query for my practice.I have two tables, one is football clubs and other is players. football clubs have next atributes: (id, name,stadium,president, founding year) and players(id,club_id,name,surname,date of birth,position) so i want to make query to show me all the clubs and players that are founded after 1902 for example.

SELECT c.name,
p.surname, p.name, p.position
FROM Clubs AS c
JOIN Players AS p ON p.club_id = c.id
WHERE c.founding_year = 1902;

Related

Referencing a past query in a loop SQLite

I want to intersect two or more results from a query from a for loop
My code takes in strings from the command line and checks the database for movies with matching actors
movie database
id | name
actor database
id | name
acting
movie_id | actor_id
i.e.
inp = sys.argv[1]
query =
'''
SELECT m.name
FROM movie m
JOIN acting ag on m.id = ag.movie_id
JOIN actor a on a.id = ag.actor_id
WHERE a.name = ?
'''
cur.execute(query, (inp,))
for tup in cur.fetchall:
print(tup)
This program would get all the movies with actor called David
./program "David'
The Secret Life of Pets
The Big Short
Concussion
A Most Violent Year
Baggage Claim
Skyfall
Drive
Albert Nobbs
The Book of Eli
Shine a Light
The Bourne Ultimatum
The Simpsons Movie
I want to extend my program to take in multiple arguments, taking in multiple names of actors, where the actors act in the same movie.
Possible code?
for index in range(len(sys.argv - 1)):
# insert code here
I think I should use an intersection of the outputs of the queries, but I don't know how to do that.
This is the output of movies that both Albert and David
./program "Albert" "David"
A Most Violent Year
Baggage Claim
The Simpsons Movie
You can use aggregation to construct a query that returns the common movies of all actors that you pass as arguments:
query = """
SELECT m.name
FROM movie m
JOIN acting ag on m.id = ag.movie_id
JOIN actor a on a.id = ag.actor_id
WHERE ',' || ? || ',' LIKE '%,' || a.name || ',%'
GROUP BY m.id, m.name
HAVING COUNT(*) = ?
"""
cur = con.cursor()
cur.execute(query, (','.join(sys.argv[1:]), len(sys.argv)-1))
for tup in cur.fetchall():
print(tup[0])

SQL Query: Select the sum(quantity) and count(number) SQLite

I am trying to query the chinnok database to select sum of quantity tracks purchase and count the number of times the track appears in a playlist in one query.
Here is what I have:
/* Query 3 : which artist has the most songs on the top 100 songs across playlist and the most sold
songs*/
SELECT ar.Name Artist_Name, tr.Name Track_Name, count(pl.Name) Play_List, pt.TrackId Track_ID,
SUM(il.Quantity) Qty,
CASE WHEN count(pl.Name)=5 THEN "Five Stars"
ELSE "Four Star" END AS Ranking
FROM Track tr
JOIN PlaylistTrack pt
ON pt.TrackId = tr.TrackId
JOIN Playlist pl
ON pl.PlaylistId=pt.PlaylistId
JOIN Album ab
ON ab.AlbumId = tr.AlbumId
JOIN Artist ar
ON ar.ArtistId = ab.ArtistId
JOIN InvoiceLine il
ON il.TrackId = tr.TrackId
GROUP BY tr.TrackId
ORDER BY Play_List DESC
LIMIT 100;
Here is the results:
Fist 6 results
First 16 results
The Qty is correct but the Play_list number is not.
Can anyone help?

SQL Query not showing results. The query is to list the names of all the actors who played in the movie 'Anand'

Please see the tables:
Sub-Query Method:
select p.Name from Person p where p.PID in
(select mc.PID from M_Cast mc where mc.MID in
(select m.MID from Movie m where lower(title)='anand' ))
Even join is not working:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
group by m.MID
having lower(m.title)='anand'
Your first query works without errors and if you make adjustments like in my few next steps the second one will work also.
Your second query: you can not select only p.Name and group by only m.MID. If it is in select clause and is not a part of aggregate function then you have to use it in group by clause. For example like this:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
group by p.Name;
Your second query also has a HAVING clause having lower(m.title)='anand' but it should be in where clause like this:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
where lower(m.title)='anand'
group by p.Name;
Now that both query are working you need to check if you have a movie with title 'ANAND' in your data. Also you need to check if you have a corresponding MID and PID data in other tables.
I have tested this in MySQL but it will maybe help you even if you use other database to guide you through some mistakes... Here is a small DEMO in MySQL where you will see that data will not be returned if there is no data in one table.
Here is a small DEMO for SQLite where you can see that your first query is working:
http://sqlfiddle.com/#!7/3ec44/1
and here is a small DEMO where you can see that my code is working:
http://sqlfiddle.com/#!7/3ec44/2
Please check the data!
After I have exchanged few comments with OP I have noticed that maybe it is a blank space in data making a problem. So I suggested this:
select p.Name
from Person p
where trim(p.PID, ' ') in (select trim(mc.PID, ' ')
from M_Cast mc
where mc.MID in (select m.MID
from Movie m
where lower(title)='anand'))
This also can be implemented in the second query:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on trim(mc.PID, ' ') = trim(p.PID, ' ')
where lower(m.title)='anand'
group by p.Name;
The problem was that in the query two tables were joined with mc.PID = p.PID and one column had data with blank spaces. So the query was trying to join this data : ' 1' = '1'. TRIM function will remove all the blank spaces in the value and join will then be possible.
SELECT MOVIE.title, person.name
FROM (MOVIE INNER JOIN mcast ON MOVIE.MID = mcast.MID) INNER JOIN person ON mcast.PID = person.PID
WHERE (((MOVIE.title)="ANAND"));
select distinct p.name [Actors in Anand]
Movie m
join M_Cast mc on mc.MID=m.MID
join Person p on p.PID=mc.PID
where m.title="Anand"
order by p.name

how to join multiple tables and extract certain column on SQLite

Here are the tables
Customer (Id, Name, Address, City, State, Zip)
Account (Id, CustomerID, OpenDate, CloseDate, Type, Balance)
Transaction (Id, AccountId, Amount, BranchId...)
Branch (Id, Name, Address, City, State, Zip)
And the question goes: List the names of the customers who made transactions of an amount exceeding $100 any bank branch in Seattle.
I am thinking about something like this, but I am not sure how to do the rest from here:
SELECT Name
FROM Customer c
JOIN Account a ON a.CustomerID = c.Id
JOIN
Please help, Thank you!!
SELECT c.Name FROM Customer c INNER JOIN Account a ON c.Id = a.CustomerID INNER JOIN TRANSACTION t on t.AccountId = a.Id INNER JOIN BRANCH b ON t.BranchID = b.Id WHERE t.AMOUNT > 100 AND t.branch = "Seattle"

Sqlite - combining 2 SELECT statements from same table

I need to combine 2 results from SELECT statements that look in the same table.
What I have works but I'm sure there is a better way to do it.
SELECT
games._id,win.winner,deal.dealer
FROM
games,
(SELECT
players.name as winner
FROM
games, players
WHERE
players._id = games.winner
AND games._id = 2) AS win,
(SELECT
players.name as dealer
FROM
games, players
WHERE
players._id = games.dealer
AND games._id = 2) AS deal
WHERE
games._id = 2
Thanks for your help optimizing this query.
EDIT:
The schema for the tables are:
CREATE TABLE games
(
_id INTEGER PRIMARY KEY,
winner integer,
dealer integer
)
CREATE TABLE players
(
_id INTEGER PRIMARY KEY,
name text
)
Something like below(untested code) should work:
SELECT g._id,p1.name AS winner,p2.name AS dealer
FROM games AS g join players AS p1 ON p1._id = g.winner
JOIN players AS p2 on p2._id = g.dealer
WHERE g._id = 2

Resources