SQL select statement

SQL Select Statement

 SQL Select Statement


SQL  select Statement most widely used statement of SQL, used to retrieve or read data from the database and data retrieved is known as result set.SQL select statement

Syntax of Select Statement

SELECT col1, col2, ...
FROM table_name;

Here Col1, Col2 are the columns we want to retrieve from table_name  Table. If you want to select all columns then follwing query will be used.

SELECT * from table_name;

Optional clauses in SELECT statement

There are some optional clauses that is used with SELECT statement:

WHERE Clause: It specifies condition on rows to retrieve.

GROUP BY Clause: Groups rows that share a property so that the aggregate function can be applied to each group.

HAVING Clause: It selects among the groups defined by the GROUP BY clause.

ORDER BY Clause: It specifies an order to retrieve the rows from database.


Now we will see and the practical example and  for that we will take a table Employee as follows.

 


Select all rows from Employee

Select * from Employee;

This will give following result :-

Results : 250 records found.
Emp_Id FirstName LastName Salary Gender Phone
101 Sam Joseph 20000 M XXX0074321
201 Rohit Puri 45000 M xxxx768675
301 Jessica Alba 50000 F xxxx870977
401 Afroz Nawaz 75000 M xxxx836343
501 Aswathy Sam 60000 F xxxx886576
…. ……. ……. …… .. ………

Select  FirstName, Salary and Gender from Employee

Select FirstName, Salary, Gender from Employee;

This will give following result :

Results : 250 records found.
FirstName Salary Gender
Sam 20000 M
Rohit 45000 M
Jessica 50000 F
Afroz 75000 M
Aswathy 60000 F

Select Using Where Clause

Here list of all Employees whose salary is greater than or Equal to 50000.

Select FirstName, Salary, Gender from Employee WHERE Salary >= 50000;

This will give following result :

Results : 123 records found.

FirstName Salary Gender
Jessica 50000 F
Afroz 75000 M
Aswathy 60000 F

Using ORDER BY Clause with Select Statement

Here select all Employees whose salary is greater than or Equal to 50000 and Order the rows in descending order on the basis of salary.

Select FirstName, Salary, Gender from Employee WHERE Salary >= 50000 ORDER BY
Salary DESC;

This will give following result :

Results : 123 records found.

FirstName Salary Gender
Afroz 75000 M
Aswathy 60000 F
Jessica 50000 F

You will see more example on select statement with other clause in their respective section.


Learn about SQL Operators 

A complete Tutorial to Learn JDBC

One Reply to “SQL Select Statement”

Leave a Reply