SELECT statement is used to fetch (retrieve) the data from the database. In this tutorial, you will learn how to retrieve the desired data from the database using SELECT statement.
Syntax:
SELECT column1, column2, ... FROM table_name;
DBLS SQL Select Examples:
Let’s say we have a table STUDENT
that has 5 rows and the table attributes are: STU_ID
, STU_NAME
, AGE
and ADDRESS
. Table has 5 rows (tuples).
Fetching the entire record (all the columns): The following SQL query will fetch all the records from the STUDENT
table.
SELECT * FROM STUDENT;
Result:
STU_ID | STU_NAME | AGE | ADDRESS |
---|---|---|---|
1001 | Carl | 21 | Delhi |
1002 | Harry | 22 | Delhi |
1003 | Lucas | 22 | Noida |
1004 | Will | 23 | Agra |
1005 | Robert | 20 | Jaipur |
Fetching selected columns: This SQL query will fetch the STU_ID
field from the table for all the students.
SELECT STU_ID FROM STUDENT;
Result:
STU_ID ------ 1001 1002 1003 1004 1005
You can use the following query to fetch STU_NAME
and ADDRESS
from the table:
SELECT STU_NAME, ADDRESS FROM STUDENT;
Result:
STU_NAME | ADDRESS |
---|---|
Carl | Delhi |
Harry | Delhi |
Lucas | Noida |
Will | Agra |
Robert | Jaipur |
Fetching selected rows and columns: To fetch the student names from those records where age is greater than 21.
SELECT STU_NAME FROM STUDENT WHERE AGE > 21 ;
Result:
STU_NAME -------- Harry Lucas Will
Leave a Reply