SELECTing specific information with SQL - sql

I created a database from the following flow-chart:
Populated according to this:
There is a book called 'The Lost Tribe'.
There is a library branch called 'Sharpstown' and one called 'Central'.
There are at least 20 books in the BOOK table.
There are at least 10 authors in the BOOK_AUTHORS table.
Each library branch has at least 10 book titles, and at least two copies of each of those titles.
There are at least 8 borrowers in the BORROWER table, and at least 2 of those borrowers have more than 5 books loaned to them.
There are at least 4 branches in the LIBRARY_BRANCH table.
There are at least 50 loans in the BOOK_LOANS table.
There must be at least one book written by 'Stephen King'
I am trying to find how many copies of the book titled The Lost Tribe are owned by the library branch whose name
is "Sharpstown". Now I know that by finding out what the BranchId of Sharpstown is I could just run the following query:
SELECT No_Of_Copies FROM BOOK_COPIES WHERE BookId = 1 and BranchId = 1
But is there a way to search by using the actual name "Sharpstown"?
Thank you for your help, I am quite new to SQL SERVER and don't know how specific one user can get with queries.

For library branch, you could use IN
SELECT No_Of_Copies FROM BOOK_COPIES
WHERE BookId = 1
AND BranchId IN (SELECT BranchId
FROM library_branch
WHERE BranchName = 'Sharpstown');

Sure it's possible.
It will look something like this if you wanna use correlated subquery:
SELECT No_Of_Copies FROM BOOK_COPIES
WHERE BranchId IN (
SELECT BranchId FROM LIBRARY_BRANCH WHERE BranchName = "Sharpstown")
AND BookId IN (
SELECT BookId FROM BOOK WHERE Title = "The Lost Tribe" )
Or you could use joins as #Sachin suggested in another answer.

You have to use Join for that requirement. You have to join BOOK_COPIES with LIBRARY_BRANCH and BOOK table, then you can find by BranchName
SELECT No_Of_Copies
FROM BOOK_COPIES inner join LIBRARY_BRANCH
on BOOK_COPIES .BranchId = LIBRARY_BRANCH.BranchId
inner join BOOK
on BOOK_COPIES.BookId = BOOK.BookId
WHERE BOOK.Title = "The Lost Tribe" and LIBRARY_BRANCH.BranchName = "Sharpstown"

try this.
SELECT No_Of_Copies FROM BOOK_COPIES
WHERE BranchId in
(
select BranchId from library_branch
where branchname like '%Sharpstown%'
)
and BookId in
(
select BookId
from book where title like
'%The Lost Tribe%'
)

SELECT No_Of_Copies FROM BOOK_COPIES
LEFT JOIN LIBRARY_BRANCH on BOOK_COPIES.BranchId=LIBRARY_BRANCH.BranchId
WHERE BookId = 1 and BranchName = 'Sharpstown'

SELECT No_Of_Copies
FROM BOOK_COPIES
WHERE BookId=(SELECT BookId FROM BOOK WHERE Title="The Lost Tribe")
AND branchid=(SELECT BranchId FROM LIBRARY_BRANCH WHERE BranchName="Sharpstown")
Use Title="The Lost Tribe" if you are looking for exact match and it will faster then using Title LIKE "%The Lost Tribe%"

In case you want to get the book id dynamically as well, you can use inner join.
SELECT BC.NOOFCOPIES FROM
BOOK_COPIES BC INNER JOIN BOOK B ON B.BOOKID = BC.BOOKID
INNER JOIN BRANCH BR ON BC.BRANCHID = BR.BRANCHID
WHERE BR.BRANCHNAME = 'SHARPSTOWN';

Related

Select information from 3 tables in SQL

I have three tables in a SQLite database: book, author and loaned_books. They look like this
book: _id | author_id | title | subject
author: _id | first_name | last_name
loaned_books: _id | book_id | loan_date | due_date
The author_id is a foreign key for the author table, and the book_id is a foreign key for the book table.
I am trying to create a query that extracts all the books (with their associated author) which are not loaned. So far, I have this query:
SELECT book.title, book.subject, author.first_name, author.last_name
FROM book, author
INNER JOIN loaned_books ON book._id != loaned_books.book_id AND book.author_id = author._id
This one does the job with the exception that all the records returned are duplicated. I have tried using DISTINCT on the column names, but the result is wrong as well.
What would be a query that returns all the books (with their associated author) that are not loaned?
The way you join book and author:
FROM book, author
is wrong because it returns the cartesian product of the 2 tables.
You need a LEFT JOIN to loaned_books and return the non matching rows (meaning the books that don't exists in loaned_books)
SELECT b.title, b.subject, a.first_name, a.last_name
FROM book AS b
INNER JOIN author AS a ON b.author_id = a._id
LEFT JOIN loaned_books AS l ON b._id = l.book_id
WHERE l._id IS NULL
Could you please try executing this:
SELECT distinct book.title, book.subject, author.first_name, author.last_name
FROM book inner join author on book.id = author._id left outer join loaned_books ON book._id = loaned_books.book_id where loaned_books.book_id is null or length(loan_date) = 0 or loan_date = ''

How to construct a SQL query involving max values?

I have the following psql tables with the following columns:
Library:
library_id
Shelf:
shelf_id
library_id (Library foreign key)
forbidden = (boolean)
Author
author_id
shelf_id (Shelf foreign key)
number_pages_witten
Book
book_id
author_id (Author foreign key)
book_name
I need a query that retrieves the following back:
For library_id=5, get the list of book names from authors who have written the most pages per shelf, for all shelves that have "forbidden" = False
I have the following so far:
SELECT name FROM Book AS b
INNER JOIN Author AS a
ON b.author_id = a.author_id
...
...
WHERE library_id=5
My sql syntax is very weak. I'm having trouble getting the author with the most number of pages on the shelf back and plug it into rest of the query. Thank you.
The requirement to get "authors who have written the most pages per shelf, for all shelves that have "forbidden" = False" is a classic usecase for the rank() window function. From there on, you just need an inner queries:
SELECT name
FROM book b
WHERE b.author_id IN (SELECT author_id
FROM (SELECT a.author_id,
RANK() OVER
(PARTITION BY a.shelf_id
ORDER BY number_pages_written DESC) rk
FROM author a
JOIN shelf s ON a.shelf_id = a.shelf_id
WHERE library_id = 5)
WHERE rk = 1)
This query simultaneously retrieve list of most pages book per author and per shelf,
At first, It's prepared a list of Author Id, Shelf Id and Number Pages Written by a self-join that has the same Author Id and Shelf Id but their Number Pages Written are different,
Then, Group by Author Id and Shelf Id
I hope it can be helpful for your question and all relate issues
select author_id1, shelf_id1, max(num_pages1)
(
select
Author1.author_id author_id1, Author1.shelf_id shelf_id1, Author1.number_pages_witten num_pages1,
Author2.author_id author_id2, Author2.shelf_id shelf_id2, Author2.number_pages_witten num_pages2
from Author Author1 left join Author Author2
on Author1.author_id = Author2.author_id and Author1.shelf_id = Author2.shelf_id
and Author1.number_pages_witten != Author2.number_pages_witten
)ds
group by author_id1, shelf_id1

How do i select datas from two different tables when count() = 2 with join?

I have a db with 3 tables, as following
CREATE TABLE Person
(id_pers number (3),
name varchar(20) NOT NULL,
phone number (15),
PRIMARY KEY(id_pers)
);
CREATE table Book(
id_book number(3),
title varchar(30),
about varchar (200),
nr_of_books number (3),
type varchar (11),
PRIMARY KEY(id_book)
);
CREATE table Author(
id_book number(3),
id_aut number(3),
FOREIGN KEY (id_book) REFERENCES Book(id_book),
FOREIGN KEY (id_aut) REFERENCES Person(id_pers)
);
I want to display the title of the book that has exactly 2 authors and the name of the authors which wrote the book. Example "Book1 - Author1, Author2"
All i managed to do is to take the book id and the number of authors but i want them more precisely.
The code that I wrote is this:
SELECT au.id_book, count(au.id_aut)
FROM author au join book bk ON au.id_book = bk.id_book
JOIN person p ON p.id_pers = au.id_aut
GROUP BY au.id_book
HAVING COUNT(au.id_aut) = 2;
everything I thought about had as result either "no group by expression" either some error because of the syntax.
Should I make a nested query? if so, what should i type in "where" to be equal to this?
I'm so confused. Any help would be appreciated.
This will include your title in the result, and it won't give errors about missing group by. I am not sure this is all you want, though...
SELECT au.id_book, bk.title, count(au.id_aut)
FROM author au
JOIN book bk
ON au.id_book = bk.id_book
JOIN person p
ON p.id_pers = au.id_aut
GROUP BY au.id_book, bk.title
HAVING COUNT(au.id_aut) = 2;
In order to include the authors names, I resorted to something a bit more elaborate:
DECLARE #result varchar(500)
DECLARE #numAut int
SET #result = ''
SET #numAut = 2
SELECT #result = #result + [Name] + ', '
FROM person WHERE id_pers in
(SELECT id_aut FROM author WHERE id_book in
( SELECT id_book FROM author
GROUP BY id_book HAVING COUNT(*) = #numAut)
);
SELECT bk.title, #result
FROM author au JOIN book bk ON au.id_book = bk.id_book
GROUP BY au.id_book, bk.title
HAVING COUNT(*) = #numAut;
First, we enumerate the names of the authors that appear in the "books with N authors"-list. This result Is then included in th (now a bit simpler) actual query, where I basically only select the titles of the books in that same list.
This works, but i am wondering if there is not a more elegant way...
SELECT au.id_book, count(au.id_aut)as count
FROM author au join book bk ON au.id_book = bk.id_book
JOIN person p ON p.id_pers = au.id_aut
GROUP BY au.id_book
HAVING (count=2)
Given your data structure, I don't think the joins are necessary.
Although your query looks fine, the following simpler version may also work:
SELECT au.id_book, count(au.id_aut)
FROM author au
GROUP BY au.id_book
HAVING COUNT(au.id_aut) = 2;
In order to display the book title and the authors you're going to need a GROUP_CONCAT:
SELECT au.id_book, bk.title, count(au.id_aut), GROUP_CONCAT(p.name) authors
FROM author au
JOIN book bk ON au.id_book = bk.id_book
JOIN person p ON p.id_pers = au.id_aut
GROUP BY au.id_book, bk.title
HAVING COUNT(au.id_aut) = 2

SQL Query Using Data from 2 tables

I have 2 tables: authors and books.
in authors i have attributes authorID, authorName, and authorDOB.
authorID is the primary key in this table.
in the books table i have attributes bookISBN, authorID, etc.
with bookISBN as the primary and authorID as the foreign key
i am trying to perform a query where given an author name, perform a
count of all the books by that author.
here is what i got:
SET #ID =
AuthorID
FROM authors
WHERE ('Mark Twain' = AuthorName);
SELECT COUNT(*)
FROM books
WHERE (AuthorID = ID);
Any help would be greatly appreciated
Try:
SELECT a.authorId, a.authorName, count(*)
FROM authors a
INNER JOIN books b ON b.AuthorID=a.AuthorID
WHERE ('Mark Twain' = a.AuthorName)
GROUP BY a.authorId, a.authorName
i am trying to perform a query where given an author name, perform a count of all the books by that author.
Try
select count(1)
from books b
inner join authors a on a.AuthorID=b.AuthorID
where a.AuthorName='Mark Twain'
You can use a function as well if you think you'd be doing the search more frequently. Just an idea.
go
create function totalbooksbyauthor (#authorname varchar(20) ) returns table
as return
select a.authorid, a.authorname, COUNT(b.bookname) bookcount
from authors a
inner join books b
on a.authorID = b.authorID
where a.authorname = #authorname
group by a.authorid, a.authorname
go
select * from totalbooksbyauthor ('Mark Twain')

SQL Query needed to get information from TWO separate tables

I am trying to create a query that will list all books by the same author. Most of the list has only one book by one author, but I want the author that has multiple books listed in db to display those book for that author.
I have two tables:
Book - AuthorID, BkTitle, etc
Author - AuthorID, AuthFName, AuthLName
I want the result to be sorted by AuthLName and the report to consist of any books in db that have same authorid.
Example result wanted:
AUTHORID BKTITLE AUTHFNAME AUTHLNAME
--------- ----------------- ------------ -----------
504 KNIGHT FREEDOM Chris Feehan
504 KNIGHT SHOWDOWN Chris Feehan
Currently, I have the following code:
select AUTHORID, BKTITLE
from BOOK
where AUTHORID in
(select AUTHORID from
(select AUTHORID,
count(*) as BOOK_COUNT
from BOOK
group by AUTHORid
order by AUTHORid )
where BOOK_COUNT >= 2);
which gives:
AUTHORID BKTITLE
---------- --------------------
504 KNIGHT FREEDOM
504 KNIGHT SHOWDOWN
I need to find a way to get the information from the Author Table and add it in this.
This should do:
SELECT b.AUTHORID, b.BKTITLE, a.AUTHFNAME, a.AUTHLNAME
FROM BOOK b
INNER JOIN AUTHOR a
ON b.AUTHORID = a.AUTHORID
AND b.AUTHORID IN
(
SELECT AUTHORID
FROM BOOK
GROUP BY AUTHORID
HAVING COUNT(AUTHORID) > 1
)
ORDER BY a.AUTHLNAME, a.AUTHFNAME
How about this - updated to use a CTE (Common Table Expression) first to figure out which authors have more than one book in the database table BOOK, and then listing only those authors and their books:
;WITH AuthorsWithMoreThanOneBook AS
(
SELECT AUTHORID, BOOK_COUNT = COUNT(*)
FROM BOOK
GROUP BY AUTHORID
HAVING BOOK_COUNT > 1)
)
SELECT
b.AUTHORID, b.BKTITLE, a.AuthFName, a.AuthLName
FROM
BOOK b
INNER JOIN
AUTHOR a ON b.AuthorID = a.AuthorID
INNER JOIN
AuthorsWithMoreThanOneBook A2 ON a.AuthorID = A2.AuthorID
ORDER BY
a.AuthLName, a.AuthFName
Update: OK, you're using Oracle .... not sure (haven't used it in ages) - but can't you just extend your original query something like this:
select bk.AUTHORID, bk.BKTITLE, a.AUTHORFNAME, a.AUTHORLNAME
from BOOK AS bk
INNER JOIN AUTHOR AS a ON a.AUTHORID = bk.AUTHORID
where bk.AUTHORID in
(select AUTHORID from
(select AUTHORID,
count(*) as BOOK_COUNT
from BOOK
group by AUTHORid
order by AUTHORid )
where BOOK_COUNT >= 2);
Not sure if/how Oracle supports those table aliases (BOOK AS bk) - but I'm pretty sure it does support it some way or another....
You can do this with only one access to each table:
SELECT * FROM
(
SELECT AuthorID, BkTitle, AuthFName, AuthLName
,COUNT(*) OVER (PARTITION BY AuthorID)
AS c
FROM BOOK
JOIN AUTHOR USING (AuthorID)
)
WHERE c > 1;