4.2.1.1 General SELECT statements
The SELECT statement
Retrieves rows from one or more tables according to given conditions.
General form:
SELECT [ ALL | DISTINCT ]
FROM
[ WHERE
[ ORDER BY [DESC]
[ GROUP BY
[ HAVING
Some SELECT statements on the Case Example
SELECT * <----------------- FROM items; | * -denotes all attributes in the table
|
Result
Query 2:
SELECT cust#,custname
FROM customers;
Result
Query 3:
SELECT DISTINCT item#
FROM ord_items;
Result
Query 4:
SELECT ord# "Order ", orddate "Ordered On" <---- FROM ord_aug; | In the result set the column headings will appear as “Order” and “Ordered On” instead of ord# and orddate.
|
Result
Query 5:
SELECT item#, descr
FROM items
WHERE price>2000;
Result
Query 6:
SELECT custname
FROM customers
WHERE city<>'Bombay';
Result
Query 7:
SELECT custname
FROM customers
WHERE UPPER(city)<>'BOMBAY';
Result
SELECT * FROM ord_aug WHERE orddate > '15-AUG-94'; <----------- |
|
FROM ord_items
WHERE qty BETWEEN 100 AND 200;
Result
Query 10:
SELECT custname FROM customers WHERE city IN ('Bombay', 'Madras'); <-------
|
The conditional expression evaluates to TRUE for those records for which the value of city field is in the list ('Bombay, 'Madras') |
Result
SELECT custname FROM customers WHERE custname LIKE 'S%' ; <------------ |
LIKE 'S%' - 'S' followed by zero or more characters |
Result
Query 12:
SELECT *
FROM ord_items
WHERE qty>100 AND item# LIKE 'SW%';
Result
Query 13:
SELECT custname
FROM customers
WHERE city='Bombay' OR city='Madras';
Result
SELECT * FROM customers WHERE city='Bombay' ORDER BY custname; <-------------------- |
|
Result
SELECT * FROM ord_items ORDER BY item#, qty DESC; <-------------
|
Display the result set in the ascending order of item#. If there are more than one records with the same item# , they will be displayed in the descending order of qty |
Result
Query 16:
SELECT descr, price ORDER BY 2 ORDER BY 2; <---------------------------- |
ORDER BY the 2nd attribute (price) in the attribute list of the SELECT clause |
Result
Query 17:
FROM ord_aug, customers WHERE city='Delhi' AND ord_aug.cust# = customers.cust#; <----------------
| SELECT statement implementing JOIN operation. JOIN condition |
Result
Query 18:
SELECT ord#, customers.cust#, city
FROM ord_aug, customers
WHERE ord_aug.cust# = customers.cust#;
Result
Query 19:
SELECT ord#, customers.cust#, city FROM ord_aug, customers WHERE ord_aug.cust# = customers.cust# (+); <----------- |
(+) indicates outer join. Here it is a right outer join as indicated by the (+) after the right side field. |
Leave a Comment