Insert, Select

INSERT

Inserting into a Table

The insert statement is used to insert or add a row of data into the table.

To insert records into a table, enter the key words insert into followed by the table name, followed by an open parenthesis, followed by a list of column names separated by commas, followed by a closing parenthesis, followed by the keyword values, followed by the list of values enclosed in parenthesis. The values that you enter will be held in the rows and they will match up with the column names that you specify. Strings should be enclosed in single quotes, and numbers should not.

Examples

INSERT INTO EMP VALUES (1,’INDU’,’MANAGER’,’12-22-2000’, 44000)

Inserting Null Value

INSERT INTO EMP (ENO, ENAME, DGN.DOJ) VALUES (13,’BAIJU’,’CLEARK’,’01-01-2008’)

SELECT

Selecting Data

The select statement is used to query the database and retrieve selected data that match the criteria that you specify. Here is the format of a simple select statement:

The column names that follow the select keyword determine which columns will be returned in the results. You can select as many column names that you'd like, or you can use a "*" to select all columns.

The table name that follows the keyword from specifies the table that will be queried to retrieve the desired results.

The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where.

Conditional selections used in the where clause:

=

Equal

>

Greater than

<

Less than

>=

Greater than or equal

<=

Less than or equal

<>

Not equal to

LIKE

*See note below

The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that allows you to select only rows that are "like" what you specify. The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified.
 

Examples

For displaying full Table

SELECT * FROM EMP

Different types of select Query

· SELECT ENAME, DGN, SAL FROM EMP

· SELECT * FROM EMP WHERE SAL>1000 AND DGN=’MANAGER’

· SELECT * FROM EMP WHERE SAL BETWEEN 10000 AND 20000


· SELECT * FROM EMP WHERE DGN IN (‘MANAGER’,’CLEARK’)


· SELECT * FROM EMP WHERE DGN NOT IN (‘MANAGER’,’CLEARK’)


· SELECT * FROM ENAME LIKE(‘B%’)

Order By

To Sort the records in ascending and descending order

SELECT * FROM EMP ORDER BY ENAME

SELECT * FROM EMP ORDER BY ENAME DESC