SQL sub-queries with no joins - sql

I am really struggling with one of the questions while revising. I guess you guys can help me out.
Here I have two tables named book and branch
Branch
Book
The question is:
List the title and author of books whose sales are greater than the
average sales. For each such book, also list the difference between
its sales and the average sales. The column of differences in the
table of results should be named "Difference".
Here is what I tried
SELECT title, authorFROM book
WHERE sales > AVG (sales) ( SELECT bookNo AS Difference
FROM book
WHERE Difference= sales-AVG(sales));

You want to create a table of results with three columns: title, author and Difference(the difference between its sales and the average sales). In sql you can do math in your select expressions, so you can just add (sales-AVG(sales)) to the list of columns. To specify the name you can use the keyword AS.
SELECT title, author, (sales-AVG(sales)) AS Difference FROM book WHERE sales>AVG(sales)

Related

How do I determine which table should be to the left of my join?

I am practicing SQL joins on learnsql.com and when joining three tables, I'm having trouble determining which table should be to the left of the join clause and which should be to the right.
Show the name of each product and its calorific value for all products that in the ‘dairy’ department.
Since the question begins by asking for the name of each product, I made the assumption that my answer should be ... FROM product p .... and then I would left join the nutrition_data and department data on it.
List all products that have fewer than 150 calories. For each product show its name (rename the column to product) and the department in which can be found (name the column department).
I interpreted the second question the same way, that it should be a list of all the products (... FROM product ...) and then the department and nutrition_data would be joined to that.
Below is a picture of the tables and the answer to the first question, the wrong answer to the second question and the right answer to the second question.
https://imgur.com/a/qLqpjGo
Because the question asked for a list of all the products, I thought the product table should be the first table after the FROM clause and then all the other tables would be joined to it. This logic worked for the first question, and since the second question is similar in nature, I assumed it was the same logic. However for the second, the solution is ... FROM department ... and then the product and nutrition_data is joined to the department table.
How do I determine which table should be to the left of my join?
"all products" is what calls an outer join: all products even when they don't have calorific information
"all products that have fewer than 150 calories" cannot show the products without calorific information. Then I see no outer join here. Except if no calorific information means no calories

BigQuery SQL Query for top products from the Merchant Center

I am running into an issue with writing an SQL query with Google Big Query. Basically looking to transfer the top products, per country, per category which are also in stock into a table.
So far I have pulled in the top products, per country, per category but the issue is with getting the 'in-stock' part added to the table. I can't find any similar keys in the schema to match them up.
Ideally the table would include:
Rank, Product Title, Country, Category, In-Stock
I would really appreciate any help on this! Thanks.
I have tried to add in a separate table that includes the 'availability' key for each product but I could not match it
You have your top_products table to check the rank that you can join to the product_inventory using the rank_id.
This join will retrieve the product_id and join that key to the products table.
After that, you get the availability information of the product and then you have all the information you require.

Filtering a MariaDB SQL table for the sum of values in a column grouped by dept_name

I have an SQL table that I'm trying to write a Query for. The table has four columns, course_id, title, dept_name and credits. Every course is different and has a unique name, but many of the courses share dept_name. Each course has between 1 and 4 credits.
Complete Table
The query I'm trying to write needs to first combine the rows that have the same dept_name, and then only display the resulting departments and credit amounts with a credit sum below 30.
By using the following Query I'm able to return the credit sum grouped by dept_name, but I can't seem to only return the dept_names with a sum of less than 30. Can anyone help me out here? I've tried adding additional WHERE statements, but I just receive errors. Please let me know if this is enough or if more info is needed.
Half-Filtered Table
Use HAVING. For example:
select deptname, sum(credits)
from bk_course
group by dept_name
having sum(credits) < 30

When I try to output the customerName and customerID of people who have rented films priced 2.6, it outputs the name and ID of everyone

SELECT customerId, customerName
FROM customerRentalinfo, film
WHERE filmPrice = 2.6;
This is the table that I have created for the movie rental system. rental id, customer id, customer name, customer email, rental date, film name, quantity, film price, total price are the entities of this table:
This is too long for a comment. You cannot answer the question with the given data model, assuming that the column names are even remotely related to what they actually represent.
In particular:
The customer is attached to rentalId, which leads to filmName.
The two tables with a price have no film id or name at all. They are just prices sitting on unidentified rows.
The data model has other issues as well:
Customer information is repeated separately for each rental. Such information should be in a single Customers table.
The price would normally be in only one table.
The films table would normally have an id and a name.
In fact, every table should have an id.
And no doubt other problems as well.
I will also throw in that you should be using proper, explicit, standard, readable JOIN syntax. Never use commas in the FROM clause.

SQL: How do I find which movie genre a user watched the most? (IMDb personal project)

I'm currently working on a personal project and I could use a little help. Here's the scenario:
I'm creating a database (MS Access) for all of the movies myself and some friends have ever watched. We rated all of our movies on IMDb and used the export feature to get all of the movie data and our movie ratings. I plan on doing some summary analysis on Excel. One thing I am interested in is the most common movie genre that each person watched. Below is my current scenario. Note that the column "const" is the movies' unique IDs. I also have individual tables for each person's ratings and the following tables are the summary tables that make up the combination of all the movies we have watched.
Here's the table I had: http://imgur.com/v5x9Dhg
I assigned each genre an ID, like this: http://imgur.com/aXdr9XI
And here is a table where I have separate instances for each movie ID and a unique genre: http://imgur.com/N0wULo8
I want to find a way to count up all of the genres that each person watches. Any advice? I would love to provide any additional information that you need!
Thank you!
You need to have at least one table which has one row per user and const (movie watched). In the 3 example tables you posted nothing shows who watched which movies, which is information you need to solve your problem. You mention having "individual tables for each person's ratings," so I assume you have that information. You will want to combine all of them though, into a table called PERSON_MOVIE or something of the like.
So let's say your second table is called GENRE and its columns are ID, Genre.
Let's say your third table is called GENRE_MOVIE and its columns are Const and ID (ID corresponds to ID on the GENRE table)
Let's say the fourth table, which you did not post, but which is required, is called PERSON_MOVIE and its columns are person, Const, rating.
You could then write a query like this:
select vw1.*, ge.genre
from (select um.person, gm.id as genre_id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) vw1
inner join (select person, max(num_of_genre) as high_count
from (select um.person, gm.id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) x
group by person) vw2
on vw1.person = vw2.person
and vw1.num_of_genre = vw2.high_count
inner join genre ge
on vw1.genre_id = ge.id
Edit re: your comment:
So right now you have multiple tables reflecting people's ratings of movies. You need to combine those into a table called PERSON_MOVIE or something similar (as in example above).
There will be 3 columns on the table: person, const, rating
I'm not sure if access supports the traditional create table as select query but ordinarily you would be able to construct such a table in the following way:
create table person_movie as
select 'Bob', const, [You rated]
from ratings_by_bob
union all
select 'Sally', const, [You rated]
from ratings_by_sally
union all
select 'Jack', const, [You rated]
from ratings_by_jack
....
If not, just combine the tables manually and add a third column as shown indicating what users are reflected by each row. Then you can run my initial query.