SQL use the term query as the name for its commands. Basically, all SQL code is written in the form of a query statement and then executed against a database.

All SQL queries perform some type of data operation such as selecting data, inserting/updating data, or creating data objects such as SQL databases and SQL tables.
Each query statement begins with a clause such as SELECT,UPDATE, CREATE or DELETE and ends with semicolon(;)

Important point to be noted is that SQL is not case sensitive language which means SELECT and select have same meaning in SQL statements but MySQL make difference in table names. So if you are working with MySQL then you need to give table names as they exist in the database.

SQL Statements

We can classify the SQL statements as follows:

Data retrievalSELECT
Data manipulation language (DML)INSERT
UPDATE
DELETE
Data definition language (DDL)CREATE
ALTER
DROP
RENAME
TRUNCATE
Transaction controlCOMMIT
ROLLBACK
SAVEPOINT
Data control language (DCL)GRANT
REVOKE

Example table to explain these commands:

We will use the following table to explain the next few chapters, assume this table name as ‘Employee‘.

EIDDepartmentNameSalary

1

ResearchRavi

22000

2

HRMike

33000

3

AdminNeel

25000

4

SalesAnna

30000

5

HRJo

20000

6

AdminSunil

25000

7

SalesMark

28000

8

ResearchBill

35000

9

HRTim

40000

10

ResearchSharan

32000

Select Statement:

SELECT QUERY allows you to retrieve records from one or more tables from database.

Syntax:

[code language=”sql”] SELECT columns FROM tables WHERE <condition>

We often us WHERE Clause with SELECT Statement:

[code language=”sql”] Where {expression | column} “operator” {expression | column}[and|or] {expression | column} “operator” {expression | column}

Examples on SELECT:


Example 1: Retrieving entire data from Employee table:

[code language=”sql”] SELECT * FROM Employee

Here * indicates all the columns of the table.

Output:

EIDDepartmentNameSalary

1

ResearchRavi

22000

2

HRMike

33000

3

AdminNeel

25000

4

SalesAnna

30000

5

HRJo

20000

6

AdminSunil

25000

7

SalesMark

28000

8

ResearchBill

35000

9

HRTim

40000

10

ResearchSharan

32000


Example 2: Retrieving the data from Employee table where the department is Research:

[code language=”sql”] SELECT * FROM Employee WHERE Department="Research"

Here * indicates all the columns of the table.

Output:

EIDDepartmentNameSalary

1

ResearchRavi

22000

8

ResearchBill

35000

10

ResearchSharan

32000


Example 1: Retrieving EID and Name from Employee table where salary is grater than 22000:

[code language=”sql”] SELECT EID, Name FROM Employee WHERE Salary>22000

Here * indicates all the columns of the table.

Output:

EIDName

2

Mike

3

Neel

4

Anna

6

Sunil

7

Mark

8

Bill

9

Tim

10

Sharan


We will see more examples on SELECT statement later with more examples.

Hey! Join Our Community

Get Quick Responses & Experts' Answers in Minutes!
Get Notified - When Answered Your Question!