Select FROM Emp : 4.1.retrieving Information From A Table
Select FROM Emp : 4.1.retrieving Information From A Table
what_to_select indicates what you want to see. This can be a list of columns, or * to
indicate “all columns.” which_table indicates the table from which you want to
retrieve data. The WHERE clause is optional. If it is present, conditions_to_satisfy
specifies one or more conditions that rows must satisfy to qualify for retrieval.
This form of SELECT is useful if you want to review your entire table.
SELECT Ename,Salary
FROM emp;
Notice that the query simply retrieves the Title column from each record, and some of
them appear more than once. To minimize the output, retrieve each unique output
record just once by adding the keyword DISTINCT:
SELECT DISTNCT Title
FROM emp;
SYMBOL MEANING
= Equal to
Greater than
The following example uses the “greater than” operator to display the employees
code, salary is greater than €20.00.
FROM EMP
SELECT *
FROM WORKSON
WHERE DUR BETWEEN 12 AND 18;
IN
The IN operator is used to test for values which are in a list. The following
query finds only the rows in the SALARY table that match up to a specific. i.e.
SALARY is either 100000 or 300000.
SELECT *
FROM EMP
WHERE SALARY IN (100000, 300000);
LIKE
The LIKE operator is used to find patterns within string attributes. Standard
SQL allows you to use the percent sign (%) and underscore (_) wildcard
characters to make matches. when the entire string is not known. % means any
and all following characters are eligible while _ means any one character may be
substituted for the underscore.
EX: Enter the following query which finds all EMPLOYEE rows whose first
names begin with the letter A.
SELECT Ename
FROM emp
WHERE Ename LIKE 'A%';
NULL and IS NOT NULL
IS NULL is used to check for a null attribute value.IS NOT NULL is used to
check for not null attribute value. In the following example, the query lists all.
SELECT *
FROM EMP
WHERE Title IS NULL;
Logical Operators
SQL allows you to have multiple conditions in a query through the use of logical
operators: AND, OR and NOT. NOT has the highest precedence, followed by AND,
and then followed by OR. However, you are strongly recommended to use parentheses
to clarify the intended meaning of the query.
AND
This logical AND connective is used to set up a query where there are two
conditions which must be met for the query to return the required row(s). The
following query displays the employee number (ENO) and the employee
name(ENAME) for which the title of employees worked (TITLE) by the
employee is greater than (aa) and the salary (SALARY) is greater than
(100000).
SELECT ENO,ENAME
FROM EMP
WHERE TITLE > ‘aa’ AND SALARY >100000;
OR
If you wanted to list the names and salary of all employees where of invoice
numbers where TITLE = 'aa' OR TITLE = 'ba' you would write the following query.
SELECT ENAME,
SALARY
FROM EMP
WHERE TITLE = 'aa' OR TITLE = 'ba';
When using AND and OR in the same query it is advisable to use parentheses to make
explicit the precedence
SELECT *
FROM EMP
SELECT*
FROM EMP