New to SQL and am having trouble with Select queries - mysql

I have 3 tables: Book, Customer and Trans. I am trying to write a select query that reports the Name and Book Title of anyone who purchased a book in September. The tables have been successfully created, and all the data is in them, but I can't get this select query to work. I have tried everything. Here is what I have:
SELECT DISTINCT c.Name, b.Title, t.TDate
FROM Customer as c, Book as b, Trans as t;
WHERE c.ID=t.ID AND b.BookID=t.BookID and t.TDate < '2003-10-01';
I'm trying to avoid duplicates as well.

SELECT c.Name, b.Title
FROM Customer as c
JOIN Trans as t ON c.ID = t.ID
JOIN Book as b ON b.BookID = t.BookID
WHERE t.TDate >= '2003-10-01' and t.TDate < '2003-11-01'
GROUP BY c.Name, b.Title

Related

fetch id who have register all adv

There are 3 types of adventures for which I used distinct function in query.
There is only one 1 customer who have booked all types of adventures.
The query i used to fetch the data is:
select c.customerid,c.name
from customer c
inner join booking b
on c.customerid = b.customerid
inner join destination d
on b.destinationid=d.destinationid
inner join adventure a
on d.adventureid=a.adventureid
group by c.customerid
having count(distinct b.bid)=(select count(*) from bid)
or count(distinct a.adventuretype)=(
select count(distinct a.adventuretype)
from adventure
)
You can get the customer ids using aggregation and having:
select b.customerid
from booking b join
destination d
on b.destinationid = d.destinationid join
adventure a
on d.adventureid = a.adventureid
group by b.customerid
having count(distinct a.advtype) = 3;
Or, if you don't want to hardcode the "3", you can use:
having count(distinct a.advtype) = (select count(distinct advtype from adventure)
I'll leave it up to you to add in the customer name (using join, exists, or in).

SQL Retrieving repeated rentals

Question:
write a SQL query that retrieves repeated rentals in which the same
customer rents the same movie more than once. if a customer rents the > same movie multiple times, the output should show this (customer's name, movie's title) combination only once
There are 3 tables:
customer table: (id, name, country, created_date)
movie table: (id, title, duration, release_year)
rental table: (id, customer_id, movie_id)
i currently have:
select customer.name, movie.title
from ((rental inner join customer on rental.customer_id = customer.id) inner join movie on rental.movie_id = movie.id)
group by customer.name, movie.title
having count(*) > 1;
but it is incorrect - any thoughts?
Rewrite your query as:
SELECT c.name, m.title
FROM rental AS r
INNER JOIN customer AS c ON r.customer_id = c.id
INNER JOIN movie AS m ON r.movie_id = m.id
GROUP BY c.name, m.title
HAVING count(*) > 1
;
Your JOIN statements has wrong syntax
EDIT: to avoid naming duplicates change GROUP BY statements:
GROUP BY c.id, m.id

How to combine two sql queries such that the output of one query becomes a condition for another

I have two SQL queries
select a.name from Subjects a join Courses b on(a.id=b.subject) group by a.name having count(b.id) >=20;
this returns names of subjects with 20 or more course offerings.
select course from course_enrolments group by course having count(student)<20;
this returns the courses with less than 20 students enrolled in it.
How can I combine both the queires so that i can get names of subjects with 20 or more offerings and less than 20 students enrolled in it?
Courses.id=Course_enrolment.course can be used to join table Courses and Course_enrolments
Any help will be appreciated.
Try this:
select a.name
from Subjects a join Courses b on(a.id=b.subject)
where b.id in (select course
from course_enrolments
group by course
having count(student)<20
)
group by a.name
having count(b.id) >=20;
Try this:
SELECT
FROM (SELECT a.name,
b.id as courseid
FROM Subjects a
JOIN Courses b
ON a.id = b.subject
GROUP BY a.name
HAVING count(a.id) >= 20) s
JOIN (SELECT course
FROM course_enrollments ce
GROUP BY course
HAVING count(*) < 20) c
ON c.course = s.courseid

mysql join 2 tables and count all rows from each table

I have table contracts c (id, exp_date) and table members m (id, cid).
I need to count all contracts and all members together joining the 2 tables on m.cid = c.id.
I tried this but obviously isn't not right as it returns a same count result from both tables
SELECT count(m.id) as totmembers , count(c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0
Results should be something like 5000 contracts, 12.000 members but i'm getting 12.000 for both totmembers and totcontracts.
Try this:
SELECT count(m.id) as totmembers , count(distinct c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0
Becouse you are created an INNER JOIN statement. Create a new query with two individual query int the SELECT list.
SELECT (SELECT ... WHERE ...) AS totmembers, (SELECT ... WHERE ...) AS totcontracts

MySQL JOIN based on sortorder field

I have a database with several users that each can have several books.
I want to get a list of users and their first book based on the (Integer) sortorder field in the table book.
Below is a example of the SQL I use now. Only problem is that it does not return the first book based on the sortorder field for each user.
SELECT
b.id, b.user_id, b.sortorder, b.title
FROM
books AS b
JOIN
books_categories AS bc
ON
(b.id = bc.book_id)
JOIN
categories AS c
ON
(c.id = bc.category_id)
JOIN
users AS u
ON
(u.id = b.user_id)
GROUP BY
b.user_id
You need to join to a subquery that selects the first book for each user.
SELECT b.id, b.user_id, b.sortorder, b.title
FROM books b
JOIN (
SELECT b2.user_id, min(b2.sortorder) sortorder
FROM books b2
GROUP BY b2.user_id
) first_books USING (user_id, sortorder);