-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.Min and Max.sql
64 lines (32 loc) · 1.24 KB
/
3.Min and Max.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- Min and Maz function
Show databases;
use book_shop;
select min(released_year) from books; -- 1945
select max(released_year) from books; -- 2017
select min(pages) from books; -- 176
select max(pages) from books; -- 634
select max(pages), title from books; -- Wrong output
-- They are are connect to each other
select * from books where pages = (select min(pages) from books);
select max(pages) from books;
select * from books where pages=634;
select title, pages from books where pages =(select max(pages) from books);
select title, pages from books where pages =(select min(pages) from books);
-- OR Using ORDER BY
select * from books order by pages desc limit 1; -- ASC
select title,pages books from books order by pages Asc limit 1;
-- Min Max with Group By
-- find the year each author published their first book
select min(released_year) from books;
select author_fname,author_lname,
min(released_year)
from books
group by author_fname,
author_lname;
select author_lname, author_fname,max(pages) from books group by author_lname, author_fname;
SELECT
CONCAT(author_fname, ' ', author_lname) AS author,
MAX(pages) AS 'longest book'
FROM books
GROUP BY author_lname,
author_fname;