Learn SQL | Quick Tutorial | Lesson 1 | SELECT, WHERE, INSERT TO etc

SQL (Structure Query Language) is used for retrieving the information from the Database. In this tutorial we will learn to create CRUD (Create, Read, Update, Delete) queries to retrieve and manipulate the database results.

Learn SQL

Let’s start:

Let assume we have an “Employee” table in our database.

Employee ID Employee Name Department City
001 Harish Sharma Information Technology Delhi
002 Girish Sharma Accounts Gurugram
003 Vishnu Kumar Marketing Delhi
004 Pooja Ahuja Information Technology Noida

 

SQL Query to retrieve all data from a table

 

In result set we will receive every single row of “Employee”.

Result Set:

Employee ID Employee Name Department City
001 Harish Sharma Information Technology Delhi
002 Girish Sharma Accounts Gurugram
003 Vishnu Kumar Marketing Delhi
004 Pooja Ahuja Information Technology Noida

 

SQL Query for a specific data from the table

 

Result Set:

Employee Name Department
Harish Sharma Information Technology
Girish Sharma Accounts
Vishnu Kumar Marketing
Pooja Ahuja Information Technology

 

Note: It’s a good practice to write the keywords in capital letters in your query.

SQL Query for retrieve the Non-Duplicate (Unique) Data

 

The above query will return the unique employee names from the table “Employee”.

WHERE Clause

 

The above query will return all the records from “Employee” table where the city will be “Delhi”

Example Result Set:

Employee ID Employee Name Department City
001 Harish Sharma Information Technology Delhi
003 Vishnu Kumar Marketing Delhi

 

AND or OR Clause

 

The above query will return the result from Employee table where city is Noida or Gurugram

Result Set:

Employee ID Employee Name Department City
002 Girish Sharma Accounts Gurugram
004 Pooja Ahuja Information Technology Noida

 

Now let’s look for AND Clause

 

The above query will return the result from Employee table with all matching records where city is Noida and Gurugram

Result Set:

Employee ID Employee Name Department City
002 Girish Sharma Accounts Gurugram
004 Pooja Ahuja Information Technology Noida

 

ORDER BY Clause

 

 

By using above query SQL will return all record in result set from the Employee table with the ascending or descending order of Department column values.

Result Set:

Employee ID Employee Name Department City
002 Girish Sharma Accounts Gurugram
001 Harish Sharma Information Technology Delhi
004 Pooja Ahuja Information Technology Noida
003 Vishnu Kumar Marketing Delhi

 

INSERT TO Clause

We used INSERT TO clause for inserting the data into the table.

Let’s look the below example

 

The above the query will insert the data for the employee “Keshav Sharma” and now the table will be updated as below

Employee ID Employee Name Department City
001 Harish Sharma Information Technology Delhi
002 Girish Sharma Accounts Gurugram
003 Vishnu Kumar Marketing Delhi
004 Pooja Ahuja Information Technology Noida

 

005 Keshav Sharma Production Ghaziabad

 

Employee ID is the auto increment column so it will update automatically. No need to insert.

Leave a Reply