1 -- Original: http://www.gibello.com/code/zql/ztest.html
2 -- Also see: http://www.gibello.com/code/zql/sqltut.html
4 CREATE TABLE EmployeeAddressTable (SSN CHAR(9),FirstName VARCHAR,LastName VARCHAR, Address VARCHAR, City VARCHAR, State VARCHAR);
5 CREATE TABLE EmployeeStatisticsTable (EmployeeIDNo INTEGER, Salary INTEGER, Benefits INTEGER, Position VARCHAR);
6 CREATE TABLE AntiqueOwners (OwnerID INT, OwnerLastName TEXT, OwnerFirstName TEXT);
7 CREATE TABLE Orders (OwnerID INT, ItemDesired TEXT);
8 CREATE TABLE Antiques (SellerID INT, BuyerID INT, Item TEXT);
9 ALTER TABLE ANTIQUES ADD (PRICE DECIMAL(8,2) NULL);
10 CREATE VIEW ANTVIEW AS SELECT ITEMDESIRED FROM ORDERS;
12 SELECT FirstName, LastName, Address, City, State FROM EmployeeAddressTable;
13 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE WHERE SALARY >= 50000;
14 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE WHERE POSITION = 'Manager';
15 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
16 WHERE SALARY > 40000 AND POSITION = 'Staff';
17 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
18 WHERE SALARY < 40000 OR BENEFITS < 10000;
19 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
20 WHERE POSITION = 'Manager' AND SALARY > 60000 OR BENEFITS > 12000;
21 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
22 WHERE POSITION = 'Manager' AND (SALARY > 50000 OR BENEFITS > 10000);
23 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
24 WHERE POSITION IN ('Manager', 'Staff');
25 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
26 WHERE SALARY BETWEEN 30000 AND 50000;
27 SELECT EMPLOYEEIDNO FROM EMPLOYEESTATISTICSTABLE
28 WHERE SALARY NOT BETWEEN 30000 AND 50000;
29 SELECT SSN FROM EMPLOYEEADDRESSTABLE WHERE LASTNAME LIKE 'L%';
30 SELECT OWNERLASTNAME, OWNERFIRSTNAME FROM ANTIQUEOWNERS, ANTIQUES
31 WHERE BUYERID = OWNERID AND ITEM = 'Chair';
32 SELECT ANTIQUEOWNERS.OWNERLASTNAME, ANTIQUEOWNERS.OWNERFIRSTNAME
33 FROM ANTIQUEOWNERS, ANTIQUES
34 WHERE ANTIQUES.BUYERID = ANTIQUEOWNERS.OWNERID AND ANTIQUES.ITEM = 'Chair';
35 SELECT DISTINCT SELLERID, OWNERLASTNAME, OWNERFIRSTNAME
36 FROM ANTIQUES, ANTIQUEOWNERS
37 WHERE SELLERID = OWNERID
38 ORDER BY OWNERLASTNAME, OWNERFIRSTNAME, SELLERID;
39 SELECT OWN.OWNERLASTNAME LastName, ORD.ITEMDESIRED ItemOrdered
40 FROM ORDERS ORD, ANTIQUEOWNERS OWN
41 WHERE ORD.OWNERID = OWN.OWNERID
42 AND ORD.ITEMDESIRED IN (SELECT ITEM FROM ANTIQUES);
43 SELECT SUM(SALARY), AVG(SALARY) FROM EMPLOYEESTATISTICSTABLE;
44 SELECT MIN(BENEFITS) FROM EMPLOYEESTATISTICSTABLE WHERE POSITION = 'Manager';
45 SELECT COUNT(*) FROM EMPLOYEESTATISTICSTABLE WHERE POSITION = 'Staff';
46 SELECT SELLERID FROM ANTIQUES, ANTVIEW WHERE ITEMDESIRED = ITEM;
47 INSERT INTO ANTIQUES VALUES (21, 01, 'Ottoman', 200.00);
48 INSERT INTO ANTIQUES (BUYERID, SELLERID, ITEM) VALUES (01, 21, 'Ottoman');
49 DELETE FROM ANTIQUES WHERE ITEM = 'Ottoman';
50 DELETE FROM ANTIQUES WHERE ITEM = 'Ottoman' AND BUYERID = 01 AND SELLERID = 21;
51 UPDATE ANTIQUES SET PRICE = 500.00 WHERE ITEM = 'Chair';
52 SELECT BUYERID, MAX(PRICE) FROM ANTIQUES GROUP BY BUYERID;
53 SELECT BUYERID, MAX(PRICE) FROM ANTIQUES GROUP BY BUYERID HAVING PRICE > 1000;
54 SELECT SELLERID FROM ANTIQUES
55 WHERE PRICE > (SELECT AVG(PRICE) + 100 FROM ANTIQUES);
56 SELECT OWNERLASTNAME FROM ANTIQUEOWNERS
57 WHERE OWNERID = (SELECT DISTINCT BUYERID FROM ANTIQUES);
58 UPDATE ANTIQUEOWNERS SET OWNERFIRSTNAME = 'John'
59 WHERE OWNERID = (SELECT BUYERID FROM ANTIQUES WHERE ITEM = 'Bookcase');
60 SELECT OWNERFIRSTNAME, OWNERLASTNAME FROM ANTIQUEOWNERS
61 WHERE EXISTS (SELECT * FROM ANTIQUES WHERE ITEM = 'Chair');
62 SELECT BUYERID, ITEM FROM ANTIQUES
63 WHERE PRICE >= ALL (SELECT PRICE FROM ANTIQUES);
64 SELECT BUYERID FROM ANTIQUES UNION SELECT OWNERID FROM ORDERS;
65 SELECT OWNERID, 'is in both Orders & Antiques' FROM ORDERS, ANTIQUES
66 WHERE OWNERID = BUYERID
68 SELECT BUYERID, 'is in Antiques only' FROM ANTIQUES
69 WHERE BUYERID NOT IN (SELECT OWNERID FROM ORDERS);