I created different tables for each authors because each author table has different column names.
Table Author
Author_ID FirstName LastName
1 Rock Smith
2 Edward Thomas
Table Author Books
Author_ID BookName
1 Book1
1 Book2
1 Book3
1 Book4
1 Book5
I want result like this
Result Table
FirstName LastName BookName
Rock Smith Book1
Rock Smith Book2
Rock Smith Book3
Rock Smith Book4
Rock Smith Book4
Table New_Authors
Author_ID Author_Table
1 Rock_Smith
2 Edward_Thomas
Talbe Rock_Smith
FirstName LastName BookName
Rock Smith Book1
Rock Smith Book2
Rock Smith Book3
Rock Smith Book4
Rock Smith Book4
Is it possible to get all the Rock_Smith table info on querying New_Authors table?
To return the data from those 2 tables, you can run this query:
SELECT a.FirstName, A.LastName, b.BookName
FROM Author A inner join AuthorBooks B on A.Author_ID = B.Author_ID
If you wanted to create a 3rd table based on the query you would do:
Step 1) Create your Table.
Step 2) Run your query:
SELECT a.FirstName, A.LastName, b.BookName
INTO [NewTable]
FROM Author A inner join AuthorBooks B on A.Author_ID = B.Author_ID
This is a simple one to many relationship achieved by a LEFT JOIN:
SELECT a.LastName, a.FirstName, b.BookName
FROM Authors a LEFT JOIN AuthorBooks b ON b.Author_ID = a.Author_ID
ORDER BY LastName, FirstName, BookName
Related
I have two tables that I want to combine (join) in SQL.
Table 1 Persons:
Person _Id
First_name
Last_name
135790
John
Smith
246801
Lucas
Williams
054953
George
Johnson
460235
Adam
White
Table 2 Loans:
Borrower_Id
resident1_id
resident2_id
135790
246801
054953
460235
054953
135790
054953
246801
135790
The expected result:
Borrower_FN
Borrower_LN
resident1_FN
resident1_LN
resident2_FN
resident1_LN
John
Smith
Lucas
Williams
George
Johnson
Adam
White
George
Johnson
John
Smith
George
Johnson
Lucas
Williams
John
Smith
How can I join it?
This is a way you can achieve the result:
select p.First_name as Borrower_FN, p.Last_name as Borrower_LN,
p2.First_name as resident1_FN, p2.Last_name as resident1_LN,
p3.First_name as resident2_FN, p3.Last_name as resident2_LN
from Loans l
inner join Persons p on p.Person_id = l.Borrower_id
inner join Persons p2 on p2.Person_id = l.resident1_id
inner join Persons p3 on p3.Person_id = l.resident2_id
How to combine two tables in SQL?
Suppose we have table called books
book_id author_id name
_______ _________ _____________
1 2 XYZ
2 1 ABC
And we have table called authors
author_id firstname surname
___________ ____________ ___________
1 Alex Woodman
2 Steve Bush
I want to combine books and authors in select query:
book_id author_id name author_name
_________ __________ __________ ______________
1 2 XYZ Steve Bush
2 1 ABC Alex Woodman
You could use the JOIN clause to merge the two tables and use the CONCAT function to concatenate name and surname of the author:
SELECT
b.book_id,
a.author_id,
b.name,
CONCAT(a.firstname, ' ', a.surname) AS author_name
FROM
books b
JOIN
author a ON b.author_id = a.author_id
Display author, title, retail and retail price of all books whose retail price is the highest for the specific author.
I have the query below. I'm kinda confused how to do a subquery in a from clause.
select lname, fname, title, retail
from author natural join bookauthor
natural join books
where retail=(select max(retail)
from books);
Below is the data from the database that I'm using
FNAME LNAME TITLE RETAIL
---------- ---------- ------------------------------ ----------
SAM SMITH BODYBUILD IN 10 MINUTES A DAY 30.95
LISA PORTER BODYBUILD IN 10 MINUTES A DAY 30.95
JANICE JONES REVENGE OF MICKEY 22
TAMARA KZOCHSKY BUILDING A CAR WITH TOOTHPICKS 59.95
TINA PETERSON DATABASE IMPLEMENTATION 55.95
JUAN ADAMS DATABASE IMPLEMENTATION 55.95
JAMES AUSTIN DATABASE IMPLEMENTATION 55.95
JACK BAKER COOKING WITH MUSHROOMS 19.95
JAMES AUSTIN HOLY GRAIL OF ORACLE 75.95
LISA WHITE HANDCRANKED COMPUTERS 25
WILLIAM WHITE HANDCRANKED COMPUTERS 25
JANICE JONES E-BUSINESS THE EASY WAY 54.5
ROBERT ROBINSON PAINLESS CHILD-REARING 89.95
OSCAR FIELDS PAINLESS CHILD-REARING 89.95
JACK BAKER PAINLESS CHILD-REARING 89.95
SAM SMITH THE WOK WAY TO COOK 28.75
ROBERT ROBINSON BIG BEAR AND LITTLE DOVE 8.95
SAM SMITH HOW TO GET FASTER PIZZA 29.95
WILLIAM WHITE HOW TO MANAGE THE MANAGER 31.95
LISA WHITE SHORTEST POEMS 39.95
20 rows selected.
You could use this:
SELECT lname, fname, title, retail
FROM author a
INNER JOIN bookauthor ba
ON a.id = ba.author_id
INNER JOIN books b
ON b.id = ba.book_id
WHERE (ba.author_id, ba.retail) IN (
SELECT ba1.author_id, MAX(b1.retail)
FROM books b1
INNER JOIN bookauthor ba1
ON ON b1.id = ba1.book_id
GROUP BY ba1.author_id
);
Do not use NATURE JOIN. This is bad way of join, for all learner and programmer.
(And change author_id, book_id to column name of your specific table)
Other way:
SELECT lname, fname, title, retail
FROM author a
INNER JOIN bookauthor ba
ON a.id = ba.author_id
INNER JOIN books b
ON b.id = ba.book_id
INNER JOIN(
SELECT ba1.author_id, MAX(b1.retail) retail
FROM books b1
INNER JOIN bookauthor ba1
ON ON b1.id = ba1.book_id
GROUP BY ba1.author_id
) mr
ON
ba.author_id = mr.author_id
AND ba.retail = mr.retail
;
This can be solved using ether an inner join:
select lname, fname, title, retail
from author natural join bookauthor
natural join books
inner join (select max(retail) as max_retail, authorid
from books
group by authorid) b
on books.authorid = b.authorid and books.retail = b.max_retail
or a correlated subquery:
select lname, fname, title, retail
from author natural join bookauthor
natural join books
where retail=(select max(retail)
from books b where b.authorid=author.authorid);
Please note, because you are using the natural join it is impossible for us to know the actual column names you used for the joins. Therefore I have assumed that the authorid foreign key is called authorid
i have a table as follows:
create table table1(id integer,firstname text,lastname text);
firstname lastname======== =========
1 ben taylor
2 rob taylor
3 rob smith
4 rob zombie
5 peter smith
6 ben smith
7 peter taylor
I want to select rows with a lastname , where the lastname must be shared by ben and rob and firstnames must be ben and rob.
Hence in the above table the result of the select query must be:
1 ben taylor
2 rob taylor
3 rob smith
6 ben smith
what must be the sql query to get the above results?
I tried - select * from table1 as a,table1 as b where a.firstname='ben' and b.firstname='rob' and a.lastname=b.lastname this joined all the resultant rows which is not what i inteneded.
You can use two filtering joins to demand that the lastname is shared with both a Ben and a Rob:
select *
from Table1 t1
join Table1 rob
on rob.firstname = 'rob'
and t1.lastname = rob.lastname
join Table1 ben
on ben.firstname = 'ben'
and t1.lastname = ben.lastname
where t1.firstname in ('ben', 'rob')
Live example at SQL Fiddle.
select *
from table1 where first_name = 'ben' or first_name = 'rob'
and last_name
in (select last_name from table1 where first_name = 'rob' and last_name
in (select last_name from table1 where first_name = 'ben'))
I would like to add the unique values of tblA to tblB without creating duplicate values based on multiple fields. In the following example, FirstName and LastName determine a duplicate, Foo and Source are irrelevant.
tblA:
FirstName LastName Foo Source
John Doe 1 A
Jane Doe 2 A
Steve Smith 3 A
Bill Johnson 2 A
tblB:
FirstName LastName Foo Source
John Doe 1 B
Bob Smith 5 B
Steve Smith 4 B
This is the result I want:
tblA:
FirstName LastName Foo Source
John Doe 1 A
Jane Doe 2 A
Steve Smith 3 A
Bill Johnson 2 A
Bob Smith 5 B
Here's an equivalent of the code I've tried:
INSERT INTO tblA
SELECT B.*
FROM tblB AS B
LEFT JOIN tblA AS A ON A.FirstName = B.FirstName AND A.LastName = B.LastName
WHERE A.FirstName IS NULL
And this is the result I get:
tblA:
FirstName LastName Foo Source
John Doe 1 A
Jane Doe 2 A
Steve Smith 3 A
Bill Johnson 2 A
John Doe 1 B
Bob Smith 5 B
Steve Smith from tblB is ignored, which is good. John Doe from tblB is added, which is bad. I've spent way too much time on this and I've inspected the data every way I can think of to ensure John Doe in tblA and tblB are the same first and last name. Any ideas on what could be going wrong?
Update: FYI, on my real tblB, about 10,000 of 30,000 should be moved to tblA. This is actually moving over 21,000. The problem is this is one step of a common process.
When I try:
SELECT tbb.*
FROM tbb
LEFT JOIN tba
ON (tbb.FirstName = tba.FirstName)
AND (tbb.LastName = tba.LastName)
WHERE (((tba.LastName) Is Null));
The only line returned is:
Bob Smith 5 B
Is it possible that John Doe has a hidden character?
Edit : Sorry, it doesn't work on Access2007
You have many way to do that :
INSERT INTO tblA
SELECT B.* FROM tblB AS B
WHERE B.firstname, B.lastname NOT IN (select firstname, lastname from tblA)
Or
INSERT INTO tblA
SELECT * FROM tblB
MINUS
SELECT * FROM tblA
This one works in Access.
You can run it to infinity - it won't add more rows than needed:
INSERT INTO tblA
SELECT B.*
FROM tblB AS B
WHERE (((B.FirstName) Not In (select firstname from tblA))
AND ((B.LastName) Not In (select firstname from tblA)))