Write nested SQL query - sql

I have 2 tables
Table1
ID Name
--------------------
1 John Carter
2 Jack Hammer
3 John Adams
4 John Doe
5 Brian Adams
Table2
ID ID_FromTable1
-----------------------------
1 2
2 3
3 1
4 1
5 1
6 2
7 3
8 1
9 1
10 5
11 4
12 5
13 4
ID in both tables is the primary key
ID_FromTable1 is the foreign key pointing to ID of Table1.
Now I do something like this:
SELECT ID
FROM Table1
WHERE Name like '%John%'
This will give me the IDs 1, 3, 4.
Now using these IDs, I want to write a query on Table2 to delete all entries where ID_FromTable1 are 1, 3, 4.
Please help me write one single SQL query to get all the IDs from Table1 where Name is 'John' and then using those IDs to delete entries from Table2.
I hope I have made the question clear. Do let me know if you need any clarification.

You can use IN with subquery:
DELETE FROM Table2
WHERE ID_FromTable1 IN ( SELECT ID
FROM Table1
WHERE Name LIKE '%John%' )

In MySQL you can do it with this join
delete table2
from table2
join table1 on table2.id_fromtable1 = table1.id
WHERE t1.Name like '%John%'

Related

Selecting Distinct IDs From a Table

I have a table that looks like such:
firstName ID
Mike 1
James 2
Mike 3
Sally 4
Emma 5
Sally 6
and am trying to get an output that returns each person who has more than 1 different ID, and what those IDs are. In my example it would be like such:
firstName ID
Mike 1
Mike 3
Sally 4
Sally 6
I am working on it and have something like what is below but it is erroring. There is something in the logic I am clearly missing but I am struggling to see what it is. Can someone point me in the direction of what is wrong here?
SELECT firstName, ID
FROM table
GROUP BY ID
HAVING COUNT(ID) > 1
You select names with more than one id using exists:
select t.*
from t
where exists (select 1
from t t2
where t2.firstName = t.firstName and t2.id <> t.id
);

Sql query with selected and splited column value from another table

I have 2 tables
First table
Id Type Value
1 2 1,2,3,5
2 1 1,3,6
3 1 2,3,1,6
Second table
Id Name
1 Leon
2 Anna
3 Biorn
4 Alex
5 Peter
6 Luis
Values in First table are Ids in Second table.
I need query that returns all names by type from the first table
For example:
Type = 1
return: Leon,Anna,Biorn,Luis
type = 2
return: Leon,Anna,Biorn,Peter
I'm trying to create a View that will look like this:
Type Name
1 Leon
1 Anna
1 Biorn
1 Luis
2 Leon
2 Anna
2 Biorn
2 Peter
So I can easily select all the names by type, but I can't figure out how to do it. Please help!
You seem to recognize that this is a poor data structure. You should have a junction table -- storing lists of integers as a delimited string is not a SQLish data structure.
Sometimes, we are stuck with other people's bad design decisions. Here is one thing you can do:
select t1.type, t2.name
from table1 t1 join
table2 t2
on ',' + t1.value + ',' like ',%' + cast(t2.id as varchar(255)) + '%,';

Replace for-loop in SQL

I have a table in a database. For example table of user IDs and right IDs:
UserId RightId
---------------
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
4 2
5 1
6 1
6 2
What is the best way to insert for each userId new rightId 4?
I heard that using while or for loops is not the best way to do such thing.
Can you please show me an example how to solve such problem with JOINs and SETs for example?
How about something like
INSERT INTO MyTable (UserID, RightID)
SELECT DISTINCT
UserID,
4
FROM MyTable
SQL Fiddle DEMO
If you simply want to change every entry in the RightId column you could try something like this:
UPDATE <table_name> SET RightId=4;

Rows to comma separated list in SQL

I have a requirement wherein the table structure I have looks like
ID Owner Id NAME
1 20 Name 1
1 21 Name 2
1 34 Name 3
2 10 Name 4
2 12 Name 5
3 100 Name 6
I need a query that would give me the results as
ID Owner ID Name
1 20 Name 1, Name2, Name 3
2 10 Name4, Name5
3 100 Name 6
Currently we do this on the codebehind, but I would ideally like to do this through SQL and see if that amounts to any performance improvement.
You did not mention your DBMS so I'm assuming PostgreSQL:
SELECT id,
min(owner_id) as lowest_id,
string_agg(name, ', ') as name_list
FROM the_table
GROUP BY id

SQL Server - How to display most recent records based on dates in two tables

I have 2 tables. I Want to list the records based on the recent date. For ex: from the following tables, I want to display ID 2 and ID 4 using a select statement. ID 2 and 4 are the most recent based on the dates from the second table. Please help me with the query. Thank you.
ID EXID PID REASON
1 1 1 XYZ
2 2 1 ABX
3 3 2 NNN
4 4 2 AAA
EXID EXDATE
1 1/1/2011
2 4/1/2011
3 3/1/2011
4 5/1/2011
Here you go, this ought to do it. Let me know if you have any questions.
SELECT
TBL.ID,
TBL.EXDATE
FROM
(
SELECT
T1.ID,
T2.EXDATE,
ROW_NUMBER() OVER(PARTITION BY T1.PID ORDER BY T2.EXDATE DESC) AS 'RN'
FROM
Table1 T1
INNER JOIN Table2 T2
ON T1.EXID = T2.EXID
) TBL
WHERE
TBL.RN = 1