Anda di halaman 1dari 6

Subject :Database_lab semester:6

Lab 4: Basic SELECT statements


The learning objectives of this lab are to :
 Retrieving Information from a Table.

4.1.Retrieving Information from a Table


The SELECT statement is used to pull information from a table. The general form of
the statement is:
SELECT What-To-select
FROM which-table
[WHERE condition-to-satisfy];

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.

4.1.1 Selecting All Data


The simplest form of SELECT retrieves everything from a table:
SELECT *
FROM emp;

This form of SELECT is useful if you want to review your entire table.

4.1.2. Selecting Particular Columns


If you do not want to see entire rows from your table, just name the columns in which
you are interested, separated by commas. For example, if you want to know when
your animals were born, select the Ename and Salarycolumns:

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;

4.1.3 Selecting rows with conditional restrictions


Numerous conditional restrictions can be placed on the selected table contents in the
WHERE clause of the SELECT statement. For example, the comparison operators
shown in Table 1 can be used to restrict output.

SYMBOL MEANING

= Equal to

< Less than

<= Less than or equal to

> Greater than


>= Greater than or equal to

< > or != Not equal to

BETWEEN Used to check if an attribute is within a range.

IN Used to check if an attribute value matches any value within a list.

LIKE Used to check if an attribute value matches a given string pattern.

IS NULL / IS Used to check if an attribute is NULL / is not NULL.


NOT NULL

We will now explore some of these conditional operators using examples.

 Greater than
The following example uses the “greater than” operator to display the employees
code, salary is greater than €20.00.

SELECT ENO, ENAME, SALARY

FROM EMP

WHERE SALARY > 200000;


 Less than
Ex: Execute the following query which produces a list of all rows in which the
TITLE is alphabetically less than EE. (Because the ASCII code value for the
letter B is greater than the value of the letter A, it follows that A is less than B.)

SELECT ENO, ENAME, TITLE


FROM EMP
WHERE TITLE < ‘EE’;
 BETWEEN
The operator BETWEEN may be used to check whether an attribute value is
within a range of values. For example, if you want to see a listing for all EMP
whose SALARY are between €300000 and €500000, use the following command
sequence:
SELECT *
FROM emp
WHERE Title BETWEEN 300000AND 500000;
EX: Write a query which displays the employee number, RESP, the hours
worked per DAY(DUR) and the date worked where the hours worked per
DAY(DUR) is between12 and 20. Hint you will need to select data from the
WORKSON table.

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

WHERE TITLE=’aa’ AND SALARY BETWEEN 150000 AND 500000 OR


TITLE=’ee’;
 NOT
The logical operator NOT is used to negate the result of a conditional expression.
If you want to see a listing of all rows for which salary is not 500000, the query
would look like:

SELECT*

FROM EMP

WHERE NOT (SALARY = 500000);

Anda mungkin juga menyukai