select a column in foreign key - sql

Suppose I have two tables in SQL where
table1 has columns - id, name, sex.
table2 has columns - eid, group, hours.
column 'eid' is the foreign key in table2 and column 'id' is the primary key on table1.
suppose I conduct a search using 'select * from table2 where hour=x' in table 2 and get some results. How can I list the names of the people (from table1) associated with the ids from the search?
GOt it!

SELECT T1.NAME FROM TABLE2 T2
INNER JOIN TABLE1 T1 ON (T1.ID=T2.EID)
WHERE T2.HOUR=X

Related

Delete records where count of a related records in another table matches a condition

I have two tables with the following schema:
table1 (
id UUID UNIQUE NOT NULL PRIMARY KEY,
...
);
table2 (
id UUID UNIQUE NOT NULL PRIMARY KEY,
table1_id UUID NOT NULL FOREIGN KEY REFERENCES table1(id),
...
);
I would like to delete all records in table 1 where the count of related records in table 2 (meaning those referencing table1 with a foreign key) equals 1. I'm not entirely sure how to do this.
Here is an invalid query I've made up that expresses what I want to do:
DELETE
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.table1_id
WHERE COUNT t2.table1_id = 1;
I am getting a syntax error with the LEFT JOIN. I'm not certain how to join on a delete.
I am using PostgreSQL 15.
A straightforward option is to filter with a correlated subquery that commutes the count of matching record in table2 for each row of table1:
delete from table1 t1
where 1 = (
select count(*) from table2 t2 where t2.table1_id = t1.id
)
The query should take advantage of the underlying index of the foreign key relationship.

Replacing the values in a column in select query

I have a table where one of the columns is ids as foreign key to another table. how can I replace the ids with data from the other table inside select query?
You use a join:
select t1.column_one, t2.display_value
from table_one t1
join table_two t2 on t1.fk_column_to_table_two = t2.primary_key_column;

How to find the common values from two different tables having a common column

I have two tables table1 and table2. Both tables have a common column named city.
How do I find all values under city which are in both the tables ?
You can do an inner join on the city column, to find values that exist in both tables.
select
-- Output the city from either table (since it will be the same)
t1.city
from
-- Join table1 and table2 together, on a matching city column
table1 t1 join table2 t2 on (t1.city=t2.city)
group by
-- Only return a single row per city
t1.city
SELECT tbone.desired_column1
tbone.desired_column2,
--other columns from table one
tbtwo.desired_column1,
tbtwo.desired_column2
--other columns from table two
-- Bellow we're stating what this table could be identified as (tbone and tbtwo), so that you don't have to keep typing table name above and bellow. Can be anything, such as A or B or HORSECORRECTINGBATTERY
FROM table1 tbone,
table2 tbtwo
WHERE tbone.city = tbtwo.city
If you don't want to specify which columns to take, just go with
SELECT * FROM ...

Get data with 2 tables

I have table1 from which i want to get some columns but column id from table1 must be same as column id from table2
Something like this:
Select title, image, price
from table1
where id = id from table2
You could use an inner join
SELECT title,image,price from table1
INNER JOIN table2 ON table1.id = table2.id
I think it's odd that your foreign key on table2 has a name (id) that imply a primary key, unless you want to join two primary keys to create a table partition.

Join tables with Two foreign keys

I am searching for a real scenario problem that I faced last night while joining two tables with foreign keys. Actually I want to get all values from second table on behalf of foreign key.
Here are my two tables let suppose:
table1 (id_user_history(PK),id_user(FK), order_no, p_quantity)
table2 (id_shoping_cart(PK), id_user(FK),order_id, prod_quantity)
Now I want to get all values from table2 by joining these tables with table1(id_user(Fk)) and table2( id_user(FK))
SELECT *
FROM table2 t2
LEFT JOIN
table1 t1
on t1.id_user = t2.id_user
all records from table 2 and only those record which match on table 1.
SQL is mainly set logic. Here's a link which helps visualize.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Looks like a simple join fits the bill:
select *
from table1 t1
left join
table2 t2
on t1.id_user = t2.id_user