Pr.Pg Next Pg

Select statement to display the data from the table tutorials

 

  • The SELECT statement retrieves or extracts information from tables in the database. Using the SELECT statement, you can choose the rows in a table that you want returned from a query by specifying values to match the rows that you want to see (or don't want to see).

  • Basic Select statement:
    In its simplest form a SELECT statement must contain the following:

         A SELECT clause, which specifies the COLUMNs containing the values to be matched against

         A FROM clause, which specifies the TABLE containing the COLUMNs listed in the SELECT clause.

  • Before applying the SELECT statement, firstly we will create the new table ITEM_MASTER.

 

The statement of creating a table (ITEM_MASTER) is

CREATE TABLE ITEM_MASTER

( ITEM_CODE CHAR(4),

ITEM_DESCR CHAR(20),

QTY_IN_HAND NUMBER(38,0),

CONSTRAINT itemcode_pk PRIMARY KEY (ITEM_CODE)

) ;

 

Now insert the data in to the ITEM_MASTER table, the statement is

INSERT INTO ITEM_MASTER

VALUES('MTR1','Samsung Monitor',25);

 

INSERT INTO ITEM_MASTER

VALUES('LP11','DELL Laptop',10);

 

INSERT INTO ITEM_MASTER

VALUES('KB01','Iball Keyboard',250);

 

INSERT INTO ITEM_MASTER

VALUES('MS11','DELL Mouse',50);

 

Example 1: Now Example of SELECT Statement:

 

SELECT * FROM ITEM_MASTER;

  • The result set will look like that:

 

  • The example above retrieves all rows from the table 'ITEM_MASTER'

Select specific columns by naming the columns to be used

Syntax:

SELECT <column_name1>, <column_name2>, . . . , <column_nameN>

FROM <table_name>;

  • Example 2:

  • For example to select only the ITEM_CODE and ITEM_DESCR from the ITEM_MASTER table, the query will be:

SELECT ITEM_CODE, ITEM_DESCR FROM ITEM_MASTER;

 

  • The result set will look like that:

 

SELECT with WHERE Clause

  • The WHERE clause is used to screen or filter records for specific matches.

  • Syntax:

SELECT * FROM <table_name> WHERE (some condition);

Example 3:

  • The following example retrieves the specific records from the ACCOUNT_MASTER1 table where COUNTRY is UK.

 

SELECT ACCOUNT_CODE, ACCOUNT_TYPE,NAME,CITY,EMAIL,COUNTRY FROM ACCOUNT_MASTER1 WHERE COUNTRY='UK';

  • The Result set will look like that:

 


Pr.Pg border                                              Next Pg