SQL select with three tables - sql

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1

You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

Related

Performing a query on a foreign table but returning only the original table?

Let's say I have a Item and Person table, where the Person 'id' column matches the Item 'person_id' column. I want to retrieve records in the Item table where the owner (Person) of the Item has 'category' 1. Sample structure below:
Item
id
item_name
person_id
Person
id
name
category (can be 1, 2, or 3)
I understand that I can use 'join' to find rows of the Item table where the two ids match, but I cannot use join for my use case. I need my SQL query to return only Item columns, and not Person columns. Basically, how can I construct a query that will query a table using values in another table, while still maintaining the original table structure?
I understand that I can use 'join' to find rows of the Item table where the two ids match, but I cannot use join for my use case. I need my SQL query to return only Item columns, and not Person columns.
This statement is just false. You can select whatever columns you want when joining:
select i.*
from items i join
persons p
on i.person_id = p.id and p.category = 1;
In terms of constructing the query, exists is also a very reasonable approach, but you can definitely use join.
One way is using EXISTS with correlated subquery
SELECT *
FROM Item i
WHERE EXISTS (SELECT 1
FROM Person p
WHERE p.id = i.person_id AND p.category = 1)
You could use IN clause to filter the person table with category 1
SELECT * FROM Item WHERE person_id IN (
SELECT Id FROM Person WHERE category=1)

tableau join multiple tables ORing on single column

I have following tables: Product and ProductEvent. Some of the relevant columns of Product table are SocialID, MarketID, LocalID. These three columns have numbers that corresponds to one column called EventID in ProductEvent.
Here is my question: is there a better way to join these tables where I do not have to make 3 separate joins all pointing to match EventID?
Maybe something like this:
select *
from Product P join ProductEvent PE
on (PE.EventID = P.SocialID or PE.EventID = P.MarketID or PE.EventID = P.LocalID)

Join SQL tables, but with additional info from joined table

Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key

Select all in table where a column in table 1 exists in another table and a second column equals a variable in second table

I know the title is confusing but its the best I could explain it. Basically im developing a cinema listings website for a company which owns two cinemas. So I have a database which has the two tables "Films" and "Listings" with data for both cinemas in them.
I'm trying to select all films and their data for one cinema if the films name shows up in the listings (since the two cinemas share all films but in the table but the may not have the same films showing)
Here is what i have come up with but I run into a problem as when the "SELECT DISTINCT" returns more than one result it obviously cant be matched with the FilmName on tbl Films.
How can i check this value for all FilmNames on tblFilms?
SELECT *
FROM tblFilms
WHERE FilmName = (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
use IN if the subquery return multiple values,
SELECT *
FROM tblFILMS
WHERE FilmName IN (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
Another way to solve thius is by using JOIN (which I recommend)
SELECT DISTINCT a.*
FROM tblFILMS a
INNER JOIN tblListings b
ON a.FilmName = b.FilmName AND
b.Cimema = 1
for faster query execution, add an INDEX on FilmName on both tables.
If you have your schemas for the tables, that would help.
That said, I believe what you want to look at is the JOIN keyword. (inner/outer/left/etc). That's exactly what JOIN is meant to do (ie your title).

Retrieve different row from same table

i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.