CREATE TABLE authors (
au_id CHAR(3) NOT NULL,
au_fname VARCHAR(15) NOT NULL,
au_lname VARCHAR(15) NOT NULL,
phone VARCHAR(12) ,
address VARCHAR(20) ,
city VARCHAR(15) ,
state CHAR(2) ,
zip CHAR(5) ,
CONSTRAINT authors_pk
PRIMARY KEY (au_id),
CONSTRAINT authors_unique1
UNIQUE (au_fname, au_lname));
INSERT INTO authors(au_id,au_fname,au_lname,phone,address,city,state,zip)
values ('A07','Paddy',"O'Furniture",'941-925-0752','1442 Main St','Sarasota','FL','34236');
CREATE TABLE publishers (
pub_id CHAR(3) NOT NULL,
pub_name VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state CHAR(2) ,
country VARCHAR(15) NOT NULL,
CONSTRAINT publishers_pk PRIMARY KEY (pub_id));
INSERT INTO publishers (pub_id,pub_name,city,state,country)
values ('P04','Tenterhooks Press','Berkeley','CA','USA');
CREATE TABLE titles(
title_id CHAR(3) NOT NULL,
title_name VARCHAR(40) NOT NULL,
type VARCHAR(10) ,
pub_id CHAR(3) NOT NULL,
pages INTEGER
CHECK (pages > 0) ,
price DECIMAL(5,2) ,
sales INTEGER ,
pubdate DATE ,
contract SMALLINT NOT NULL,
CONSTRAINT pk_titles PRIMARY KEY (title_id),
CONSTRAINT title_id_chk
CHECK (
(SUBSTR(title_id, 1, 1) = 'T')
AND (CAST(SUBSTR(title_id, 2, 2) AS SIGNED) BETWEEN 0 AND 99)),
CONSTRAINT price_chk CHECK (price >= 0.00 AND price < 100.00),
CONSTRAINT sales_chk CHECK (sales >= 0),
CONSTRAINT pubdate_chk CHECK (pubdate >= '1950-01-01'),
CONSTRAINT title_name_chk CHECK (title_name <> ''AND contract >= 0));
INSERT INTO titles (title_id,title_name,type,pub_id,pages,price,sales,pubdate,contract)
values ('T13',"What Are The Civilian Applications?",'history','P03','802','29.99','10467','1999-05-31','1');
CREATE TABLE title_authors (
title_id CHAR(3) NOT NULL,
au_id CHAR(3) NOT NULL,
au_order SMALLINT NOT NULL,
royalty_share DECIMAL(5,2) NOT NULL,
CONSTRAINT title_authors_pk
PRIMARY KEY (title_id, au_id),
CONSTRAINT title_authors_fk1
FOREIGN KEY (title_id)
REFERENCES titles(title_id),
CONSTRAINT title_authors_fk2
FOREIGN KEY (au_id)
REFERENCES authors (au_id));
INSERT INTO title_authors (title_id,au_id,au_order,royalty_share)
values ('T13','A01',1,1.00);</h6>
CREATE TABLE royalties (
title_id CHAR(3) NOT NULL,
advance DECIMAL(9,2),
royalty_rate DECIMAL(5,2),
CONSTRAINT royalties_pk PRIMARY KEY (title_id),
CONSTRAINT royalties_title_id_fk
FOREIGN KEY (title_id)
REFERENCES titles(title_id));
INSERT INTO royalties (title_id,advance,royalty_rate)
values ('T13',20000.00,0.06);
CREATE TABLE test_scores (
name varchar(20), test varchar(20), score tinyint );
INSERT INTO test_scores VALUES
("Steve", "SQL", 75),
("Robert", "SQL", 43),
("Tracy", "SQL", 56),
("Tatiana", "SQL", 87),
("Steve", "Tuning", 83),
("Robert", "Tuning", 31),
("Tracy", "Tuning", 88),
("Tatiana", "Tuning", 83);
CREATE TABLE IF NOT EXISTS emp (
empno DECIMAL(4), ename VARCHAR(10), job VARCHAR(9),
mgr DECIMAL(4), hiredate CHAR(10), sal DECIMAL(7,2),
comm DECIMAL(7,2), deptno DECIMAL(2),
CONSTRAINT pk_emp PRIMARY KEY (empno));
INSERT INTO emp VALUES
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400,30),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1987-07-13', 3000, NULL, 20),
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30),
(7876, 'ADAMS', 'CLERK', 7788, '1987-07-13', 1100, NULL, 20),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10) ;
CREATE TABLE new_publishers (
pub_id CHAR(3) NOT NULL,
pub_name VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state CHAR(2),
country VARCHAR(15) NOT NULL,
CONSTRAINT publishers_pk PRIMARY KEY (pub_id));
INSERT INTO new_publishers (pub_id,pub_name,city,state,country)
values ('P11','Community Press','Los Angeles','CA','USA'),
('P12','Wiley and Sons','Pittsburgh','PA','USA'),
INSERT INTO new_publishers (pub_id,pub_name,city,country)
VALUES ('P13','Verlang','Berlin','Germany'), ('P14','Springer','Berlin','Germany');
CREATE TABLE employee (
emp_id CHAR(3) NOT NULL,
emp_name VARCHAR(40) NOT NULL,
boss_id CHAR(3),
CONSTRAINT pk_employee PRIMARY KEY (emp_id),
CONSTRAINT emp_id_chk
CHECK ((SUBSTR(emp_id, 1, 1) = 'E') AND
(CAST(SUBSTR(emp_id, 2, 2) AS SIGNED) BETWEEN 0 AND 99)),
CONSTRAINT boss_id_chk
CHECK ((SUBSTR(boss_id, 1, 1) = 'E') AND (CAST(SUBSTR(boss_id, 2, 2)
AS SIGNED) BETWEEN 0 AND 99)));
INSERT INTO employee (emp_id,emp_name)
VALUES ('E01','Lord Copper');
INSERT INTO employee (emp_id,emp_name,boss_id)
VALUES ('E02','Jocelyn Hitchcock','E01'),
('E03','Mr. Salter','E01'),('E04','William Boot','E03'),('E05','Mr. Corker','E03');
SELECT city FROM authors;
SELECT au_fname, au_lname, city, state FROM authors;
SELECT city, state, country FROM publishers;
SELECT * FROM authors;
5. List authors' first name, last name, city, state, and postcode by changing the name of all columns except state.
SELECT au_fname AS "First name",au_lname AS "Last name",city AS "City", state, zip "Postal code" FROM authors;
SELECT state FROM authors;
SELECT DISTINCT state FROM authors;
SELECT city, state FROM authors;
SELECT DISTINCT city, state FROM authors;
SELECT au_fname, au_lname, city, state FROM authors ORDER BY au_lname ASC;
SELECT au_fname, au_lname, city, state FROM authors ORDER BY au_lname DESC;
12. List name, surname, city, and state of authors in ascending order by state and descending order by city.
SELECT au_fname, au_lname, city, state FROM authors ORDER BY state ASC, city DESC;
SELECT city, state FROM authors ORDER BY zip ASC;
SELECT title_id, price, sales, price * sales AS Revenue FROM titles ORDER BY Revenue DESC;
SELECT title_id, price,sales, price * sales AS Revenue FROM titles ORDER BY Revenue IS NULL DESC, Revenue DESC;
SELECT au_id, au_fname, au_lname FROM authors WHERE au_lname <> 'Hull';
SELECT title_name, contract FROM titles WHERE contract = 0
SELECT title_name, pubdate FROM titles WHERE pubdate >= '2001-01-01';
SELECT title_name, price * sales AS "Revenue" FROM titles WHERE price * sales > 1000000;
SELECT sales AS copies_sold FROM titles WHERE copies_sold > 100000;
SELECT title_name, type, price FROM titles WHERE type = 'biography' AND price < 20;
22. List authors whose surname begins with a letter between H and Z and whose status is not in California.
SELECT au_fname, au_lname FROM authors WHERE au_lname >= 'H' AND au_lname <= 'Zz' AND state <> 'CA';
SELECT au_fname, au_lname, city, state FROM authors WHERE state in ('NY', 'CO') OR city='San Francisco';
SELECT pub_id, pub_name, state, country FROM publishers;
SELECT pub_id, pub_name, state, country FROM publishers WHERE (state = 'CA') OR (state <> 'CA');
SELECT au_fname, au_lname, state FROM authors WHERE state <> 'CA';
SELECT title_name, sales, price FROM titles WHERE NOT (price < 20) AND (sales > 15000);
SELECT title_id, type, price FROM titles WHERE type = 'history' OR type = 'biography' AND price < 20;
SELECT title_id, type, price FROM titles WHERE (type = 'history' OR type = 'biography') AND price < 20;
SELECT type, type = 'history' AS "Hist?", type = 'biography' AS "Bio?", price, price < 20 AS "<20?" FROM titles;
- List authors whose surname begins with Kel
SELECT au_fname, au_lname FROM authors WHERE au_lname LIKE 'Kel%';
SELECT au_fname, au_lname FROM authors WHERE au_lname LIKE '__ll%';
SELECT au_fname, au_lname, city, state, zip FROM authors WHERE zip LIKE '94___';
SELECT au_fname, au_lname, phone FROM authors WHERE phone NOT LIKE '212-___-____' AND phone NOT LIKE '415-___-%' AND phone NOT LIKE '303-%';
SELECT title_name FROM titles WHERE title_name LIKE '%!%%' ESCAPE '!';
SELECT au_fname, au_lname, zip FROM authors WHERE zip NOT BETWEEN '20000' AND '89999';
SELECT title_id, price FROM titles WHERE price BETWEEN 10 AND 19.95;
38. From the titles table, print the title_id columns starting with T1 and having another character, ignoring possible leading and trailing spaces.
SELECT title_id, pubdate FROM titles WHERE pubdate BETWEEN '2000-01-01' AND '2000-12-31';
SELECT title_id, price FROM titles WHERE (price > 10) AND (price < 19.95);
40. List authors who do not live in any of these states: New York (NY), New Jersey (NJ), or California (CA). (NY), New Jersey (NJ), or California (CA).
SELECT au_fname, au_lname, stateFROM authors WHERE state NOT IN ('NY', 'NJ', 'CA');
41. List the title of the books together with their length for those books whose length is less than 30. The list has to be sorted by the length of the title.
SELECT title_id, advance FROM royalties WHERE advance IN (0.00, 1000.00, 5000.00);
SELECT title_id, pubdate FROM titles WHERE pubdate IN ('2000-01-01', '2001-01-01', '2002-01-01');
43. List 1) the titles that contain the letter u in their first 10 positions and 2) the position of the letter u itself. The resulting table has to be sorted in descending order by the position of the letter u.
SELECT pub_id, city, state, country FROM publishers WHERE state = 'CA';
44. Print books that have been published in the first 6 months of the year 2001 or 2002. The output has to be sorted in descending order of date.
SELECT pub_id, city, state, country FROM publishers WHERE state <> 'CA';
SELECT pub_id, city, state, country FROM publishers WHERE state <> 'CA';
SELECT title_id, type, pubdate FROM titles WHERE type = 'biography' AND pubdate IS NOT NULL;
SELECT au_id, 2 + 3 FROM authors;
SELECT title_id, price, 0.10 AS "Discount", price * (1 - 0.10) AS "New price" FROM titles;
SELECT title_id, advance AS "Advance" FROM royalties;
50. Listar libros de biografĂas junto con las ventas que han generado (venta = price*sales) en orden descendiente
SELECT title_id,price * sales AS Revenue FROM titles WHERE type = 'biography' ORDER BY Revenue DESC;
SELECT title_id,pages,pages DIV 10 AS "pages/10",pages/10.0 AS "pages/10.0" FROM titles;
SELECT 2 + 3 * 4 AS "2+3*4", (2 + 3) * 4 AS "(2+3)*4", 6 / 2 * 3 AS "6/2*3", 6 / (2 * 3) AS "6/(2*3)" ;
SELECT au_fname || ' ' || au_lname AS "Author name" FROM authors ORDER BY au_lname ASC, au_fname ASC;
SELECT SALES || ' copies sold of title ' || title_id AS 'Biography sales' FROM titles WHERE type = 'biography' AND sales IS NOT NULL ORDER BY sales DESC;
SELECT 'Title '|| title_name || ' published on ' || pubdate AS "Biography sales" FROM titles WHERE type = 'biography' AND pubdate IS NOT NULL ORDER BY pubdate DESC;
SELECT au_id, au_fname, au_lname FROM authors WHERE au_fname || ' ' || au_lname = 'Klee Hull';
SELECT pub_id,SUBSTR(pub_id, 1, 1) AS "Alpha part", SUBSTR(pub_id, 2) AS "Num part" FROM publishers;
SELECT SUBSTR(au_fname, 1, 1) || '. ' || au_lname AS "Author name", state FROM authorsm WHERE state IN ('NY', 'CO');
SELECT au_fname, au_lname, phone FROM authors WHERE SUBSTR(phone, 1, 3)='415';
SELECT LOWER(au_fname) AS "Lower", UPPER(au_lname) AS "Upper" FROM authors;
SELECT title_name FROM titles WHERE UPPER(title_name) LIKE '%MO%';
SELECT '<' || ' AAA ' || '>' AS "Untrimmed", '<' || TRIM(LEADING FROM ' AAA ') || '>' AS "Leading", '<' || TRIM(TRAILING FROM ' AAA ') || '>' AS "Trailing", '<' || TRIM('AAA ') || '>' AS "Both" ;
SELECT au_lname, TRIM(LEADING 'H' FROM au_lname) AS "Trimmed name" FROM authors;
64. From the titles table, print the title_id columns starting with T1 and have another character ignoring possible leading and trailing spaces. End
SELECT title_id FROM titles WHERE TRIM(title_id) LIKE 'T1_';
SELECT au_fname, LENGTH(au_fname) AS "Len" FROM authors;
66. List the title of the books together with their length for those books whose length is less than 30.whose length is less than 30. The list has to be sorted by the length of the title.
SELECT title_name,LENGTH(title_name) AS "Len" FROM titles WHERE LENGTH(title_name) < 30 ORDER BY LENGTH(title_name) ASC;
67. List the position of the substring e in the author's first name and the position of the substring ma in the author's last name.
SELECT au_fname, LOCATE('e', au_fname) AS "Pos e", au_lname, LOCATE('ma', au_lname) AS "Pos ma" FROM authors;
68. List 1. the titles containing the letter u in their first 10 positions and 2) the position of the letter u itself. The resulting table has to be sorted in descending order by the position of the letter u.
SELECT title_name, LOCATE('u', title_name) AS "Pos" FROM titles WHERE LOCATE('u', title_name) BETWEEN 1 AND 10 ORDER BY LOCATE('u', title_name) DESC;
69. Print books that have been published in the first 6 months of the years 2001 or 2002. The output has to be sorted in descending order of date.
SELECT title_id, pubdate FROM titles WHERE YEAR(pubdate) BETWEEN 2001 AND 2002 AND MONTH(pubdate) BETWEEN 1 AND 6 ORDER BY pubdate DESC;
SELECT CURRENT_DATE AS "Date", CURRENT_TIME AS "Time", CURRENT_TIMESTAMP AS "Timestamp";
SELECT CURRENT_USER AS "User";
SELECT price AS "price(DECIMAL)", CAST(price AS INTEGER) AS "price(INTEGER)", '<' || CAST(price AS CHAR(8)) || '>' AS "price(CHAR(8))" FROM titles;
73. In the titles table, convert the column sales to CHAR(8) and the column title_-.name column to CHAR(20) for use in a concatenation with characters.
74. Increase the price of books: history books by 10%, psychology books by 20%, and leave the rest unchanged.
SELECT title_id, type, price, CASE WHEN type = 'history' THEN price * 1.10 WHEN type = 'psychology' THEN price * 1.20 ELSE price END AS "New price" FROM titles WHERE price is NOT NULL ORDER BY type ASC, title_id ASC;
75. List the books with a sales category of type characters depending on the value of the sales column.
SELECT title_id, CASE WHEN sales IS NULL THEN 'Unknown' WHEN sales <= 1000 THEN 'Not more than 1,000' WHEN sales <= 10000 THEN 'Between 1,001 and 10,000' WHEN sales <= 100000 THEN 'Between 10,001 and 100,000' WHEN sales <= 1000000 THEN 'Between 100,001 and 1,000,000' ELSE 'Over 1,000,000' END AS "Sales category" FROM titles ORDER BY sales ASC;
SELECT pub_id, city, COALESCE(state, 'N/A') AS "state", country FROM publishers;
SELECT title_id, contract, NULLIF(contract, 0) AS "Null contract" FROM titles;
SELECT MIN(price) AS "Min price" from titles;
SELECT MIN(pubdate) AS "Earliest pubdate" from titles;
SELECT MIN(pages) AS "Min history pages" FROM titles WHERE type = 'history';
SELECT MIN(price) AS "Min price", MAX(price) AS "Max price",MAX(price) - MIN(price) AS "Range" FROM titles;
SELECT title_name, MAX(price * sales) AS "Max history revenue" FROM titles WHERE type = 'history';
SELECT SUM(advance) AS "Total advances" FROM royalties;
SELECT SUM(sales) AS "Total sales (2000 books)" FROM titles WHERE year(pubdate) = 2000;
SELECT SUM(price) AS "Total price", SUM(sales) AS "Total sales", SUM(price * sales) AS "Total revenue" FROM titles;
SELECT AVG(price * 2) AS "AVG(price * 2)" FROM titles;
SELECT AVG(sales) AS "AVG(sales)",SUM(sales) AS "SUM(sales)" FROM titles WHERE type = 'history';
88. Print the books and their sales for all books whose sales are above average sales exceed the average.
SELECT title_id, sales FROM titles WHERE sales > (SELECT AVG(sales) FROM titles) ORDER BY sales DESC;
SELECT AVG(COALESCE(sales, 0)) AS AvgSales FROM titles WHERE type = 'biography';
SELECT COUNT(title_id) AS "COUNT(title_id)", COUNT(price) AS "COUNT(price)", COUNT(*) AS "COUNT(*)" FROM titles;
91. Print the count of the book titles, their price, and the quantity excluding books whose price is null
SELECT COUNT(title_id) AS "COUNT(title_id)", COUNT(price) AS "COUNT(price)", COUNT(*) AS "COUNT(*)" FROM titles WHERE price IS NOT NULL;
92. Print the count of the book titles, their price, and the quantity for those books whose price is null
SELECT COUNT(title_id) AS "COUNT(title_id)", COUNT(price) AS "COUNT(price)", COUNT(*) AS "COUNT(*)" FROM titles WHERE price IS NULL;
SELECT COUNT(*) AS "COUNT(*)" FROM titles;
94. Consider the books that do not have NULL as a price and print 1) the number of them, 2) the sum of their prices, and 3) the average of their prices.
SELECT COUNT(price) AS "COUNT(price)",SUM(price) AS "SUM(price)", AVG(price) AS "AVG(price)" FROM titles;
95. Consider the books that do not have NULL as a price and print 1) the number of them, 2) the sum of their prices, and 3) the average of their prices.
SELECT COUNT(DISTINCT price) AS "COUNT(DISTINCT)", SUM(DISTINCT price) AS "SUM(DISTINCT)", AVG(DISTINCT price) AS "AVG(DISTINCT)" FROM titles;
96. For each author, print the number of books he/she wrote, including those in which he/she is a co-author.
SELECT au_id,COUNT(*) AS "num_books" FROM title_authors GROUP BY au_id;
SELECT state, COUNT(state) AS "COUNT(state)", COUNT(*) AS "COUNT(*)" FROM publishers GROUP BY state;
98. In order to have consistent mathematical results, it is necessary to use COUNT(sales) instead of COUNT(*).
SELECT type, SUM(sales)/COUNT(sales) AS "SUM/COUNT(sales)", SUM(sales)/COUNT(*) AS "SUM/COUNT(*)", AVG(sales) AS "AVG(sales)" FROM titles GROUP BY type;
SELECT type, SUM(sales) AS "SUM(sales)", AVG(sales) AS "AVG(sales)", COUNT(sales) AS "COUNT(sales)" FROM titles GROUP BY type;
100. For each type of book, calculate the following statistics: (1) total sales, 2) average sales, and 3) number of books. The result has to be sorted by total sales. In addition, only books with a price equal to or higher than only those books whose price is equal to or greater than 13. (Use WHERE and ORDER BY).
SELECT type, SUM(sales) AS "SUM(sales)", AVG(sales) AS "AVG(sales)", COUNT(sales) AS "COUNT(sales)" FROM titles WHERE price >= 13 GROUP BY type ORDER BY SUM(sales) DESC;
101. For each publisher and type of book it lists the number of books sorted in ascending order by publisher and in descending order by number of books.
SELECT pub_id, type, COUNT(title_id) AS count FROM titles GROUP BY pub_id, type ORDER BY pub_id ASC, count DESC;
SELECT DISTINCT type FROM titles;
SELECT price, AVG(sales) FROM titles WHERE price IS NOT NULL GROUP BY price ORDER BY price ASC;
SELECT au_id,COUNT(*) AS "num_books" FROM title_authors GROUP BY au_id HAVING COUNT(*) >= 3;
105. For each type of book, it prints the number of books and the average profit (price*sales), but only for those types with an average sale of more than 1,000,000.
SELECT type, COUNT(price) AS "COUNT(price)", AVG(price * sales) AS "AVG revenue" FROM titles GROUP BY type HAVING AVG(price * sales) > 1000000;
106. For each publisher, list the number of books of each type, for publishers with more than one book per type.
SELECT pub_id, type, COUNT(*) AS count FROM titles GROUP BY pub_id, type HAVING COUNT(*) > 1 ORDER BY pub_id ASC, count DESC;
107. For books from publishers P03 and P04, list total sales and average price by type, for types with more than 10,000 total sales and less than 20 average.
###### SELECT type, SUM(sales) AS "SUM(sales)", AVG(price) AS "AVG(price)" FROM titles WHERE pub_id IN ('P03', 'P04') GROUP BY type HAVING SUM(sales) > 10000 AND AVG(price) < 20;
publisher.
SELECT au_id, authors.city FROM authors INNER JOIN publishers ON authors.city = publishers.city;
109. Select first and last names of authors who live in a city where at least one publisher also lives.
SELECT au_fname, au_lname, a.city FROM authors AS a INNER JOIN publishers p ON a.city = p.city;
110. Select first name (au_fname), last name (au_lname), and city (au_lname) for authors who live in a city in which at least one publisher also lives.
SELECT au_fname, au_lname, a.city FROM authors a INNER JOIN publishers p ON a.city = p.city;
111. Displays the selected columns of all possible combinations of rows of the authors and publishers tables.
SELECT au_id, pub_id, a.state AS "au_state", p.state AS "pub_state" FROM authors a CROSS JOIN publishers p ORDER BY au_id, pub_id;
SELECT title_id, pub_id, pub_name FROM publishers NATURAL JOIN titles;
113. For each book, list the name of the publisher (pub_name) and the advance (advance) as long as the advance is less than 20,000. Tables publishers, titles, and royalties.
SELECT title_id, pub_id, pub_name, advance FROM publishers NATURAL JOIN titles NATURAL JOIN royalties WHERE advance < 20000;
114. Join two tables (authors and title_authors) using the au_id column to list the title_id of the books each author wrote.
SELECT a.au_id, a.au_fname, a.au_lname, ta.title_id FROM authors a INNER JOIN title_authors ta ON a.au_id = ta.au_id ORDER BY a.au_id ASC, ta.title_id ASC;
115. Join three tables (authors, title_authors, and titles) using the column au_id and title_id to list the title_name of the books each author wrote.
SELECT a.au_id, a.au_fname, a.au_lname, t.title_name FROM authors a INNER JOIN title_authors ta ON a.au_id = ta.au_id INNER JOIN titles t ORDER BY a.au_id ASC, ta.title_id ASC;
116. Join two tables (titles and publishers) by means of the pub_id column to list 1) the title_id of the book, 2) the title_name of the book, 3) the pub_id of the publisher, and 4) the publisher's pub_name.
SELECT t.title_id, t.title_name, t.pub_id, p.pub_name FROM titles t INNER JOIN publishers p ON p.pub_id = t.pub_id ORDER BY t.title_name ASC;
SELECT a.au_id, a.au_fname, a.au_lname, a.city, a.state FROM authors a INNER JOIN publishers p ON a.city = p.city AND a.state = p.state ORDER BY a.au_id ASC;
118. Combines an inner join with WHERE conditions to list books published in the state of California (CA) or outside the North American countries (USA, Canada, Mexico).
SELECT t.title_id, t.title_name, p.state, p.country FROM titles t INNER JOIN publishers p ON t.pub_id = p.pub_id WHERE p.state = 'CA' OR p.country NOT IN ('USA', 'Canada', 'Mexico') ORDER BY t.title_id ASC;
119. Combines an inner join with the COUNT aggregate function and a GROUP BY clause to list the number of books an author wrote (or co-wrote).
SELECT a.au_id, COUNT(ta.title_id) AS "Num books" FROM authors a INNER JOIN title_authors ta ON a.au_id = ta.au_id GROUP BY a.au_id ORDER BY a.au_id ASC;
SELECT t.title_id, t.title_name, r.advance FROM royalties r INNER JOIN titles t ON r.title_id = t.title_id WHERE t.type = 'biography' AND r.advance IS NOT NULL ORDER BY r.advance DESC;
121. Use aggregate functions and the GROUP BY clause to list the amount and the advance paid for each type of book the advance paid for each type of book. Royalty and title tables.
SELECT t.type, COUNT(r.advance) AS "COUNT", SUM(r.advance) AS "advance" FROM royalties r INNER JOIN titles t ON r.title_id = t.title_id WHERE r.advance IS NOT NULL GROUP BY t.type ORDER BY t.type ASC;
122. Use aggregate functions and the GROUP BY clause to list the amount and advance paid for each book type and publisher.Tables royalties and titles.
SELECT t.type, t.pub_id, COUNT(r.advance) AS "COUNT", SUM(r.advance) AS "advance" FROM royalties r INNER JOIN titles t ON r.title_id = t.title_id WHERE r.advance IS NOT NULL GROUP BY t.type, t.pub_id ORDER BY t.type ASC, t.pub_id ASC;
123. List title_id and number of co-authors of all books written by 2 or more authors by 2 or more authors.
SELECT ta.title_id, COUNT(ta.au_id) AS "authors" FROM authors a INNER JOIN title_authors ta ON a.au_id = ta.au_id GROUP BY ta.title_id HAVING COUNT(ta.au_id) > 1 ORDER BY ta.title_id ASC;
SELECT t.title_id, t.title_name, r.advance, t.price * t.sales AS "Revenue" FROM titles t INNER JOIN royalties r ON t.price * t.sales > r.advance * 10 AND t.title_id = r.title_id ORDER BY t.price * t.sales DESC;
SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a INNER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC;
126. Left outer join to include all authors in the result regardless of whether there is an editor living in the same city. whether or not there is an editor living in the same city.
SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a LEFT OUTER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC;
SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a RIGHT OUTER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC;
127. Right outer join to include all publishers in the result regardless of whether there is an author living in the same city.
SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a RIGHT OUTER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC;
128. Full outer join to include all authors and publishers in the result regardless of whether an author lives in the same city as a publisher.
FULL OUTER JOIN is not implemented in MariaDB.
SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a LEFT OUTER JOIN publishers p ON a.city = p.city UNION DISTINCT SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a RIGHT OUTER JOIN publishers p ON a.city = p.city ORDER BY pub_name ASC, au_lname ASC, au_fname ASC;
130. Lists the number of books written by each author including authors who have not written any books
SELECT a.au_id, COUNT(ta.title_id) AS "Num books" FROM authors a LEFT OUTER JOIN title_authors ta ON a.au_id = ta.au_id GROUP BY a.au_id ORDER BY a.au_id ASC;
SELECT a.au_id, a.au_fname, a.au_lname FROM authors a LEFT OUTER JOIN title_authors ta ON a.au_id = ta.au_id WHERE ta.au_id IS NULL;
132. It combines an inner join and a left outer join to list all authors and any possible books they have written along with their sales. But it shows sales only for those books that sold more than 100,000 copies.
SELECT a.au_id aid, a.au_fname fname, a.au_lname lname, COALESCE(tta.title_id, 'N/A') t_id, COALESCE(tta.title_name, 'N/A') name, COALESCE(tta.sales, 'N/A') sales FROM authors a LEFT OUTER JOIN (SELECT ta.au_id, t.title_id, t.title_name, t.sales FROM title_authors ta INNER JOIN titles t ON t.title_id = ta.title_id WHERE sales > 100000) tta ON a.au_id = tta.au_id ORDER BY a.au_id ASC, tta.title_id ASC;
SELECT ta.au_id, t.title_id, t.title_name, t.sales FROM title_authors ta INNER JOIN titles t ON t.title_id = ta.title_id WHERE sales > 100000;
SELECT e1.emp_name AS "Employee name", e2.emp_name AS "Boss name" FROM employee e1 INNER JOIN employee e2 ON e1.boss_id = e2.emp_id;
SELECT a1.au_id, a1.au_fname, a1.au_lname, a1.state FROM authors a1 INNER JOIN authors a2 ON a1.state = a2.state WHERE a2.au_id = 'A04';
135. For each biography, it lists its id (title_id), its sales, and the other biographies that sold more than it. biographies that sold more than it.
SELECT t1.title_id, t1.sales, t2.title_id AS "Better seller", t2.sales AS "Higher sales" FROM titles t1 INNER JOIN titles t2 ON t1.sales < t2.sales WHERE t1.type = 'biography' AND t2.type = 'biography' ORDER BY t1.title_id ASC, t2.sales ASC;
SELECT a1.au_fname, a1.au_lname, a2.au_fname, a2.au_lname FROM authors a1INNER JOIN authors a2 ON a1.state = a2.state WHERE a1.state = 'NY' ORDER BY a1.au_id ASC, a2.au_id ASC;
SELECT a1.au_fname, a1.au_lname, a2.au_fname, a2.au_lname FROM authors a1 INNER JOIN authors a2 ON a1.state = a2.state AND a1.au_id <> a2.au_id WHERE a1.state = 'NY' ORDER BY a1.au_id ASC, a2.au_id ASC;
138. Lists the different pairs of authors living in New York without redundancies and without duplicate pairs.
SELECT a1.au_fname, a1.au_lname, a2.au_fname, a2.au_lname FROM authors a1 INNER JOIN authors a2 ON a1.state = a2.state AND a1.au_id < a2.au_id WHERE a1.state = 'NY' ORDER BY a1.au_id ASC, a2.au_id ASC;
SELECT DISTINCT pub_id FROM titles WHERE type = 'biography';
SELECT DISTINCT pub_name FROM publishers p INNER JOIN titles t ON p.pub_id = t.pub_id WHERE t.type = 'biography';
SELECT pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE type = 'biography');
SELECT au_id, city FROM authors WHERE city IN (SELECT DISTINCT city FROM publishers);
SELECT au_id, au_fname, au_lname FROM authors WHERE au_id NOT IN (SELECT au_id FROM title_authors);
SELECT au_id, au_fname, au_lname, state FROM authors WHERE state IN (SELECT state FROM authors WHERE au_id = 'A04');
SELECT t.title_id, t.type, t.sales FROM titles AS t WHERE sales >= (SELECT AVG(sales) FROM titles AS av WHERE av.type = t.type);
UPDATE titles SET contract = 0 ;
UPDATE titles SET type = 'self help', pages = NULL WHERE type = 'psychology';
UPDATE titles SET sales = sales * 0.5 WHERE sales > ( SELECT upper FROM ( SELECT AVG(sales) as upper FROM titles) as avgg );
UPDATE titles SET pubdate = '2003-01-01' WHERE title_id IN (SELECT title_id FROM title_authors WHERE au_id IN (SELECT au_id FROM authors WHERE au_fname = 'Sarah' AND au_lname = 'Buchman'));
UPDATE titles SET pub_id = (SELECT pub_id FROM publishers WHERE pub_name = 'Abatis Publishers') WHERE pub_id = (SELECT pub_id FROM publishers WHERE pub_name = 'Tenterhooks Press');
CREATE VIEW au_names AS SELECT au_id, au_fname, au_lname FROM authors;
184. Create a view with authors living in a city where there is at least one publisher. at least one publisher.
CREATE VIEW cities (au_id, au_city, pub_id, pub_city) AS SELECT a.au_id, a.city, p.pub_id, p.city FROM authors a INNER JOIN publishers p ON a.city = p.city;
CREATE TABLE test_scores ( name varchar(20), test varchar(20), score tinyint );
INSERT INTO test_scores VALUES ("Steve", "SQL", 75), ("Robert", "SQL", 43), ("Tracy", "SQL", 56), ("Tatiana", "SQL", 87), ("Steve", "Tuning", 83), ("Robert", "Tuning", 31), ("Tracy", "Tuning", 88), ("Tatiana", "Tuning", 83);
SELECT name, test, score, AVG(score) OVER (PARTITION BY test) AS avgtest FROM test_scores;
SELECT name, test, score, AVG(score) OVER (PARTITION BY name) AS avgname FROM test_scores;
SELECT name, test, score, AVG(score) OVER (PARTITION BY test) AS avgtest, AVG(score) OVER (PARTITION BY name) AS avgname FROM test_scores;
SELECT row_number() OVER (order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT rank() OVER (order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT dense_rank() OVER (order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT row_number() OVER (partition by test order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT rank() OVER (partition by test order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT dense_rank() OVER (partition by test order BY score DESC) AS pos, name, test, score FROM test_scores;
SELECT name, test, score, lead(score) OVER (partition by test order BY score DESC) AS next FROM test_scores;
SELECT name, test, lag(score) OVER (partition by test order BY score DESC) AS prev, score FROM test_scores;
SELECT name, test, lag(score) OVER (partition by test order BY score DESC) AS prev, score, lead(score) OVER (partition by test order BY score DESC) AS next FROM test_scores;
CREATE TABLE IF NOT EXISTS emp ( empno DECIMAL(4), ename VARCHAR(10), job VARCHAR(9), mgr DECIMAL(4), hiredate CHAR(10), sal DECIMAL(7,2), comm DECIMAL(7,2), deptno DECIMAL(2), CONSTRAINT pk_emp PRIMARY KEY (empno));
INSERT INTO emp VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20), (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30), (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30), (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20), (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400,30), (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30), (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10), (7788, 'SCOTT', 'ANALYST', 7566, '1987-07-13', 3000, NULL, 20), (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10), (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30), (7876, 'ADAMS', 'CLERK', 7788, '1987-07-13', 1100, NULL, 20), (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30), (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20), (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10) ;
SELECT empno, ename, deptno, sal, AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal FROM emp;
SELECT empno, ename, deptno, sal, AVG(sal) OVER () AS avg_dept_sal FROM emp;
SELECT empno, ename, deptno, sal, MIN(sal) OVER (PARTITION BY deptno order by sal) AS min_dept_sal FROM emp ;
202. Salary of an employee together with the difference between his or her salary and the department's minimum wage.
SELECT empno, ename, deptno, sal, sal - MIN(sal) OVER (PARTITION BY deptno order by sal) AS diff_min FROM emp ;
SELECT empno, hiredate, ename, sal, MAX(sal) OVER (order by hiredate) AS max FROM emp ;
204. Salary of an employee together with the average between the salary 1) of the employee hired before, 2) of the employee himself/herself, and 3) of the employee hired after.
SELECT empno, hiredate, ename, sal, AVG(sal) OVER (order by hiredate rows between 1 preceding and 1 following ) AS avg FROM emp ;
205. Salary of an employee together with the average between the salary 1) of the employees hired on the last date that there was hiring prior to the employee's 2) of the employee himself/herself and the employees hired on the same day, and 3) of the employees hired on the next date of hire after the employee's date of hire.
CREATE VIEW IF NOT EXISTS ranked as SELECT dense_rank() OVER( ORDER BY hiredate ) rank, empno, hiredate, ename, sal FROM emp ;
SELECT * FROM ranked ;
SELECT empno, hiredate, ename, sal, AVG(sal) OVER (order by rank range between 1 preceding and 1 following ) AS avg FROM ranked ;
206. For each employee, average wages of employees hired before, employee's wages, average wages of employees hired after.
SELECT empno, hiredate, ename, avg(sal) OVER (order by hiredate rows between unbounded preceding and 1 preceding ) AS avgBefore, sal, avg(sal) OVER (order by hiredate rows between 1 following and unbounded following ) AS avgAfter FROM emp ;