Sunteți pe pagina 1din 3

USE [LibraryDb]

GO

-- 1. Afișați titlul și descrierea tuturor cărților.


SELECT [Books].[Title],
[Books].[Description]
FROM [dbo].[Books]
GO

-- 2. Afișați numărul total de cărți.


SELECT COUNT(*) AS NumberOfBooks
FROM [dbo].[Books]
GO

-- 3. Afișați toate cărțile publicate în 2019 ordonate după titlu.


SELECT [Books].[BookID],
[Books].[Title],
[Books].[Published],
[Books].[Price]
FROM [dbo].[Books]
WHERE DATEPART(YEAR FROM [Books].[Published])=2019
ORDER BY [Books].[Title] ASC
GO

-- 4. Afișați toate cărțile publicate la editura Polirom în iunie 2018 ordonate după titlu.
SELECT [Books].[Title],
[Books].[Published],
[Publishers].[Name] AS Publisher
FROM [Books]
JOIN [Publishers] ON [Publishers].[PublisherID]=[Books].[PublisherID]
WHERE DATEPART(MONTH FROM [Books].[Published])=6 AND
DATEPART(YEAR FROM [Books].[Published])=2018 AND
[Publishers].[Name]='Polirom'
ORDER BY [Books].[Title] ASC
GO

-- 5. Afișați toate cărțile din categoria Mister, publicate la editura Rao, ordonate descrescător după dată.
SELECT [Books].[Title],
[Genres].[Name] AS Genre,
[Publishers].[Name] AS Publisher,
[Books].[Published]
FROM [Books]
JOIN [Publishers] ON [Publishers].[PublisherID]=[Books].[PublisherID]
JOIN [Genres] ON [Genres].[GenreID]=[Books].[GenreID]
WHERE [Genres].[Name]='Mister' AND
[Publishers].[Name]='Rao'
ORDER BY [Books].[Published] DESC
GO

-- 6. Afișați toate cărțile scrise de Stephen King.


SELECT [Books].[Title],
[Authors].[FirstName] + ' ' + [Authors].[LastName] AS Author
FROM [Books]
JOIN [BooksAuthors] ON [BooksAuthors].[BookID]=[Books].[BookID]
JOIN [Authors] ON [Authors].[AuthorID]=[BooksAuthors].[AuthorID]
WHERE [Authors].[FirstName]='Stephen' AND
[Authors].[LastName]='King'
GO

-- 7. Afișați câte cărți a scris Mircea Eliade.


SELECT COUNT(*)
FROM [BooksAuthors]
--JOIN [BooksAuthors] ON [BooksAuthors].[BookID]=[Books].[BookID]
JOIN [Authors] ON [Authors].[AuthorID]=[BooksAuthors].[AuthorID]
WHERE [Authors].[FirstName]='Mircea' AND
[Authors].[LastName]='Eliade'
GO

-- 8. Afișați numarul de cărți din fiecare categorie.


SELECT [Genres].[Name] AS Genre,
COUNT([Books].[BookID])
FROM [Books]
RIGHT JOIN [Genres] ON [Genres].[GenreID]=[Books].[GenreID]
GROUP BY [Genres].[Name]
GO

-- 9. Afișați numarul de cărți pentru fiecare autor. Se va afișa numele complet al autorului și numarul de
cărți.
SELECT [Authors].[FirstName] + ' ' + [Authors].[LastName] AS Author,
COUNT([Books].[BookID]) AS NumberOfBooks
FROM [Books]
JOIN [BooksAuthors] ON [BooksAuthors].[BookID]=[Books].[BookID]
JOIN [Authors] ON [Authors].[AuthorID]=[BooksAuthors].[AuthorID]
GROUP BY [Authors].[FirstName],
[Authors].[LastName]
GO

-- 10. Afișați ce categorii de cărți a scris Agatha Christie.


SELECT [Genres].[Name] AS Genre,
COUNT([Books].[BookID]) AS NumberOfBooks
FROM [Books]
JOIN [Genres] ON [Genres].[GenreID]=[Books].[GenreID]
JOIN [BooksAuthors] ON [BooksAuthors].[BookID]=[Books].[BookID]
JOIN [Authors] ON [Authors].[AuthorID]=[BooksAuthors].[AuthorID]
WHERE [Authors].[FirstName]='Agatha' AND
[Authors].[LastName]='Christie'
GROUP BY [Genres].[Name]
GO

-- 11. Afișați pretul mediu al unei cărți pentru fiecare editura.


SELECT [Publishers].[Name],
AVG([Books].[Price]) AS MeanPrice
FROM [Books]
JOIN [Publishers] ON [Publishers].[PublisherID]=[Books].[PublisherID]
GROUP BY [Publishers].[Name]

-- 12. Afișați pretul mediu al unei cărți pentru fiecare autor.


SELECT [Authors].[FirstName] + ' ' + [Authors].[LastName] AS Author,
AVG([Books].[Price]) AS MeanPrice
FROM [Books]
JOIN [BooksAuthors] ON [BooksAuthors].[BookID]=[Books].[BookID]
JOIN [Authors] ON [Authors].[AuthorID]=[BooksAuthors].[AuthorID]
GROUP BY [Authors].[FirstName],
[Authors].[LastName]
GO

-- 13. Afișați cărțile Polițiste care au preț mai mare decât media prețurilor pentru cărțile Polițiste de la editura
Aldo Press.
DECLARE @MeanPrice float;
SET @MeanPrice = (SELECT AVG([Books].[Price])
FROM [Books]
JOIN [Genres] ON [Genres].[GenreID]=[Books].
[GenreID]
JOIN [Publishers] ON [Publishers].[PublisherID]=[Books].
[PublisherID]
WHERE [Genres].[Name]='Polițist' AND
[Publishers].[Name]='Aldo Press');
SELECT @MeanPrice;
SELECT [Books].[Title],
[Books].[Price],
[Genres].[Name] AS Genre,
[Publishers].[Name] AS Publisher
FROM [Books]
JOIN [Genres] ON [Genres].[GenreID]=[Books].[GenreID]
JOIN [Publishers] ON [Publishers].[PublisherID]=[Books].[PublisherID]
WHERE [Genres].[Name]='Polițist' AND
[Books].[Price]>@MeanPrice
GO

-- 14. Afișați primele 4 cărți ca volume avute în inventar pentru o anumită librărie.
SELECT TOP 5 [Books].[Title],
[Inventories].[NumberOfBooks],
[Libraries].[LibraryName]
FROM [Books]
JOIN [Inventories] ON [Inventories].[BookID]=[Books].[BookID]
JOIN [Libraries] ON [Libraries].[LibraryID]=[Inventories].[LibraryID]
WHERE [Libraries].[LibraryName]='Cărturești'
ORDER BY [Inventories].[NumberOfBooks] DESC
GO

S-ar putea să vă placă și