Автор: Пользователь скрыл имя, 16 Декабря 2011 в 07:17, реферат
Большинство основанных на SQL баз данных предоставляют специальные средства, позволяющие совершенствовать вывод ваших запросов. Конечно, они претерпевают значительные изменения от программы к программе, и их обсуждения здесь не будет, однако имеются пять особенностей, созданных в стандарте SQL, которые позволяют вам делать нечто большее, чем просто вывод значений полей и агрегатных данных.
3. SELECT *
FROM Customers
WHERE rating > 100
OR city = 'Rome';
или
SELECT *
FROM Customers
WHERE NOT rating < = 100
OR city = 'Rome';
или
SELECT *
FROM Customers
WHERE NOT (rating < = 100
AND city < > 'Rome');
Могут быть и другие решения.
4. onum
amt odate cnum
snum
3001
18.69 10/03/1990 2008 1007
3003
767.19 10/03/1990 2001 1001
3005
5160.45 10/03/1990 2003 1002
3009
1713.23 10/04/1990 2002 1003
3007
75.75 10/04/1990 2004 1002
3008
4723.00 10/05/1990 2006 1001
3010
1309.95 10/06/1990 2004 1002
3011 9891.88 10/06/1990 2006 1001
5. onum amt odate cnum snum
3001 18.69 10/03/1990 2008 1007
3003
767.19 10/03/1990 2001 1001
onum
amt odate
cnum snum
3006 1098.16
10/03/1990 2008 1007
3009 1713.23
10/04/1990 2002 1003
3007 75.75
10/04/1990 2004 1002
3008 4723.00
10/05/1990 2006 1001
3010 1309.95
10/06/1990 2004 1002
3011 9891.88 10/06/1990 2006 1001
6. SELECT *
FROM Salespeople;
1. SELECT *
FROM Orders
WHERE odate IN (10/03/1990,10/04/1990);
и
SELECT *
FROM Orders
WHERE odate BETWEEN 10/03/1990 AND 10/04,1990;
2. SELECT *
FROM Customers
WHERE snum IN (1001,1004);
3. SELECT *
FROM Customers
WHERE cname BETWEEN 'A' AND 'H';
4. SELECT *
FROM Customers
WHERE cname LIKE 'C%';
5. SELECT *
FROM Orders
WHERE amt < > O
AND (amt IS NOT NULL);
или
SELECT *
FROM Orders
WHERE NOT (amt = O
OR amt IS NULL);
1. SELECT COUNT(*)
FROM Orders
WHERE odate = 10/03/1990;
2. SELECT COUNT (DISTINCT city)
FROM Customers;
3. SELECT cnum, MIN (amt)
FROM Orders
GROUP BY cnum;
4 SELECT MIN (cname)
FROM Customers
WHERE cname LIKE 'G%';
5. SELECT city,
MAX (rating)
FROM Customers
GROUP BY city;
6 SELECT odate, count (DISTINCT snum
FROM Orders
GROUP BY odate;
1. SELECT onum, snum, amt * .12
FROM Orders;
2. SELECT 'For the city ', city, ', the highest rating is ', ",
MAX (rating)
FROM Customers
GROUP BY city;
3 SELECT rating, cname, cnum
FROM Customers
ORDER BY rating DESC;
4. SELECT odate, SUM (amt)
FROM Orders
GROUP BY odate
ORDER BY 2 DESC;
1. SELECT onum, cname
FROM Orders, Customers
WHERE Customers.cnum = Orders.cnum;
2. SELECT onum, cname, sname
FROM Orders, Customers, Salespeople
WHERE Customers.cnum = Orders.cnum
AND Salespeople.snum = Orders.snum;
3. SELECT cname, sname, comm
FROM Salespeople, Customers
WHERE Salespeople.snum = Customers.snum
AND comm * .12;
4. SELECT onum, comm * amt
FROM Salespeople, Orders, Customers
WHERE rating > 100
AND Orders.cnum = Customers.cnum
AND Orders.snum = Salespeople.snum;
1. SELECT first.sname, second.sname
FROM Salespeople first, Salespeople second
WHERE first.city = second.city
AND first.sname < second.sname;
Псевдонимам не обязаны иметь именно такие имена.
2. SELECT cname, first.onum, second.onum
FROM Orders first, Orders second, Customers
WHERE first.cnum = second.cnum
AND first.cnum = Customers.cnum
AND first.onum < second.onum;
Ваш вывод может иметь некоторые отличия, но в вашем ответе все логические
компоненты должны быть такими же.
3. SELECT a.cname, a.city
FROM Customers a, Customers b
WHERE a.rating = b.rating
AND b.cnum = 2001;
1. SELECT *
FROM Orders
WHERE cnum =
(SELECT cnum
FROM Customers
WHERE cname = 'Cisneros');
или
SELECT *
FROM Orders
WHERE cnum IN
(SELECT cnum
FROM Customers
WHERE cname = 'Cisneros');
2. SELECT DISTINCT cname, rating
FROM Customers, Orders
WHERE amt >
(SELECT AVG (amt)
FROM Orders)
AND Orders.cnum = Customers.cnum;
3 SELECT snum, SUM (amt)
FROM Orders
GROUP BY snum
HAVING SUM (amt) >
(SELECT MAX (amt)
FROM Orders);
1. SELECT cnum, cname
FROM Customers outer
WHERE rating =
(SELECT MAX (rating)
FROM Customers inner
WHERE inner.city = outer.city);
2. Решение с
помощью соотнесенного
SELECT snum, sname
FROM Salespeople main
WHERE city IN
(SELECT city
FROM Customers inner
WHERE inner.snum < > main.snum);
Решение
с помощью объединения:
SELECT DISTINCT first.snum, sname
FROM Salespeople first, Customers second
WHERE first.city = second.city
AND first.snum < > second.snum;
1. SELECT *
FROM Salespeople first
WHERE EXISTS
(SELECT *
FROM Customers second
WHERE first.snum = second.snum
AND rating = 300);
2. SELECT a.snum, sname, a.city, comm
FROM Salespeople a, Customers b
WHERE a.snum = b.snum
AND b.rating = 300;
3. SELECT *
FROM Salespeople a
WHERE EXISTS
(SELECT *
FROM Customers b
WHERE b.city = a.city
AND a.snum < > b.snum);
4. SELECT *
FROM Customers a
WHERE EXISTS
(SELECT *
FROM Orders b
WHERE a.snum = b.snum
AND a.cnum < > b.cnum)
1. SELECT *
FROM Customers
WHERE rating > = ANY
(SELECT rating
FROM Customers
WHERE snum = 1002);
2. cnum
cname city rating
snum
2002
Giovanni Rome 200
1003
2003
Liu San Jose 200
1002
2004
Grass Berlin 300
1002
2008 Cisneros SanJose 300 1007
3. SELECT *
FROM Salespeople
WHERE city < > ALL
(SELECT city
FROM Customers);
или
SELECT *
FROM Salespeople
WHERE NOT city = ANY
(SELECT city
FROM Customers);
4. SELECT *
FROM Orders
WHERE amt > ALL
(SELECT amt
FROM Orders a, Customers b
WHERE a.cnum = b.cnum
AND b.city = 'London');
5. SELECT *
FROM Orders
WHERE amt >
(SELECT MAX (amt)
FROM Orders a, Customers b
WHERE a.cnum = b.cnum
AND b.city = 'London');
1. SELECT cname, city, rating, 'High Rating'
FROM Customers
WHERE rating > = 200
UNION
SELECT cname, city, rating, ' Low Ratlng'
FROM Customers
WHERE rating < 200;
или
SELECT cname, city, rating, 'High Rating'
FROM Customers
WHERE rating > = 200
UNION
SELECT cname, city, rating, ' Low Rating'
FROM Customers
WHERE NOT rating > = 200;
2. SELECT cnum, cname
FROM Customers a
WHERE 1 <