How do I select the results that were inserted? - sql

I have two tables in a SQLite database.
First table:
id | name | number
1 | Paul | 1
2 | John | 2
3 | Jessica | 3
Second table:
id | name | number
1 | Aaron | 1
2 | Barbara | 2
3 | Erik | 3
I do a JOIN LEFT and I insert the result in the second table, so:
First table:
id | name | number
1 | Paul | 1
2 | John | 2
3 | Jessica | 3
Second table:
id | name | number
1 | Aaron | 1
2 | Barbara | 2
3 | Erik | 3
4 | Paul | 1
5 | John | 2
6 | Jessica | 3
After, in other sql statement, I select the results which were inserted in the second table ( 4 | Paul | 1; 5 | John | 2 and 6 | Jessica | 3).
Can i do this in one sql statement to get a better performance?
Thanks

Related

How to select 2 data from 1 column based on other relations in other table

Say, I have a table like the following called "name":
| nid | name |
----------------
| 1 | john |
| 2 | mike |
| 3 | tom |
| 4 | jack |
| 5 | will |
| 6 | david | ...
and another table like the following called "relation_father_son":
| rid | fnid | snid |
---------------------
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 4 | 5 |
| 4 | 2 | 6 | ...
then I would like a result like the following:
| father | son |
------------------
| john | mike |
| john | tom |
| jack | will |
| mike | david | ...
What the should the SQL query be?
The query would be:
SELECT
f.name AS father,
s.name AS son
FROM relation_father_son
INNER JOIN name AS f
ON (nid = fnid)
INNER JOIN name AS s
ON (nid = snid)
First of all, it is confusing that the first table is named as name. You should rename it to a more distinguished name, such as family_names. Read more at: Is name a reserved word in MySQL?
For the desired result, you can use the following query:
SELECT
(SELECT `name` FROM `family_names` WHERE nid=fnid) AS father,
(SELECT `name` FROM `family_names` WHERE nid=snid) AS son
FROM relation_father_son

How to (recursively) update a table based on available hierarchy?

Given these 3 column values, how can i update a table with the top level head for each employee?
| EmpID | EmpName | SupervisorID | DeptHeadID |
|:-----:|:-------:|:------------:|:----------:|
| 3 | Adam | null | |
| 1 | Sam | 5 | |
| 6 | Mike | 2 | |
| 5 | Jack | 3 | |
| 2 | Steph | 5 | |
| 8 | Rob | 2 | |
The result should be like this
| EmpID | EmpName | SupervisorID | DeptHeadID |
|:-----:|:-------:|:------------:|:----------:|
| 3 | Adam | null | null |
| 1 | Sam | 5 | 3 |
| 6 | Mike | 2 | 3 |
| 5 | Jack | 3 | 3 |
| 2 | Steph | 3 | 3 |
| 8 | Rob | 2 | 3 |
Example
;with cteP as (
Select EmpID,SupervisorID,TopLvl=EmpID
From YourTable
Where SupervisorID is null
Union All
Select r.EmpID,r.SupervisorID,TopLvl=p.TopLvl
From YourTable r
Join cteP p on r.SupervisorID = p.EmpID)
Update YourTable
set DeptHeadID = nullIf(TopLvl,A.EmpID)
From YourTable A
Join cteP B on A.EmpID=B.EmpID
-- Show Updated Table
Select * From YourTable
Updated Table
EmpID EmpName SupervisorID DeptHeadID
3 Adam NULL NULL
1 Sam 5 3
6 Mike 2 3
5 Jack 3 3
2 Steph 5 3
8 Rob 2 3

How to structure a proper SQL subquery?

I'm trying to wrap my head around how to do a proper subquery, it's not making sense to me, lets say I have two tables books and chapters:
Books
+----+------------------+----------+---------------------+
| id | name | author | last_great_chapters |
+----+------------------+----------+---------------------+
| 1 | some book title | john doe | 2 |
| 2 | foo novel title | some guy | 4 |
| 3 | other book title | lol man | 3 |
+----+------------------+----------+---------------------+
Chapters
+----+---------+----------------+
| id | book_id | chapter_number |
+----+---------+----------------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 1 | 4 |
| 4 | 1 | 5 |
| 5 | 2 | 1 |
| 6 | 2 | 2 |
| 7 | 2 | 3 |
| 8 | 2 | 4 |
| 9 | 2 | 5 |
| 10 | 3 | 1 |
| 11 | 3 | 2 |
| 12 | 3 | 3 |
| 13 | 3 | 4 |
| 14 | 3 | 5 |
+----+---------+----------------+
How can I join the two tables, and just print out the number of rows (sorted limit(last_great_chapters)) of the "last_great_chapters" from the books table list for each book?
if I understood correctly, you want to print out table books and last_great_chapters count in Chapters table?
if yes, try it
select b.id, b.name, b.author , b.last_great_chapter, COUNT(c.chapter_number) as rownumbers FROM Books as b
LEFT JOIN Chapters AS C ON c.chapter_number = b.last_great_chapters
group by b.id, b.name, b.author , b.last_great_chapter

Percentage to total in BigQuery Legacy SQL (Subqueries?)

I can't understand how to calulate percentage to total in BigQuery Legacy SQL.
So, I have a table:
ID | Name | Group | Mark
1 | John | A | 10
2 | Lucy | A | 5
3 | Jane | A | 7
4 | Lily | B | 9
5 | Steve | B | 14
6 | Rita | B | 11
I want to calculate percentage like this:
ID | Name | Group | Mark | Percent
1 | John | A | 10 | 10/(10+5+7)=45%
2 | Lucy | A | 5 | 5/(10+5+7)=22%
3 | Jane | A | 7 | 7/(10+5+7)=33%
4 | Lily | B | 9 | 9/(9+14+11)=26%
5 | Steve | B | 14 | 14/(9+14+11)=42%
6 | Rita | B | 11 | 11/(9+14+11)=32%
My table is quite long for me (3 million rows).
I thought that I could do it with subqueries, but in SELECT I can't use subqueries.
Does anyone know a way to do it?
SELECT
ID, Name, [Group], Mark,
RATIO_TO_REPORT(Mark) OVER(PARTITION BY [Group]) AS percent
FROM YourTable
Check more about RATIO_TO_REPORT

Selecting Multiple ID's in one Select

I have a Database with entries that have to be grouped togethe
id | Name | Surname | Time
1 | Michael | Kane | 3
2 | Torben | Dane | 4
3 | Dinge | Chain | 5
4 | Django | Fain | 5
5 | Juliett | Bravo | 6
6 | Django | Fain | 7
7 | Django | Fain | 3
8 | Django | Fain | 4
9 | Dinge | Chain | 4
10 | Torben | Dane | 4
Now I want to group the items while maintaing all Id's. I'm comming close with the following query but I am lossing my ids
SELECT id, Name, Surname, sum(Time) from Names group by(Name)
The Result of the Query is
id | Name | Surname | Time
9 | Dinge | Chain | 9
8 | Django | Fain | 19
5 | Juliett | Bravo | 6
1 | Michael | Kane | 3
10 | Torben | Dane | 8
while I would need all ids like this
ids | Name | Surname | Time
3,9 | Dinge | Chain | 9
4,6,78 | Django | Fain | 19
5 | Juliett | Bravo | 6
1 | Michael | Kane | 3
2,10 | Torben | Dane | 8
How can i accomplish this?
You would do this using group_concat():
select group_concat(id, ',') as ids, name, surname, sum(time) as time
from table t
group by name, surname;
Just don't store the results back in the database. Comma-separated values are useful for returning results, but it is the wrong format for storing data in the database.