SQL Tutorial For Beginners | Learn SQL

Welcome to SQL Tutorial. You will learn introduction to SQL, history of SQL, Uses of SQL and much more. Let’s start!!!

Stay updated with latest technology trends
Join DataFlair on Telegram!!

What is SQL?

SQL, which goes by the full form – Structured Query Language is programming, or we can say a query language that is used to deal with the relational database operations and queries which could involve manipulating or updating a database or creating a new database.

Major operations on a database are known as CRUD operations i.e., Create, Read, Update and Delete and are a widely used database in the technological world.

The database is required to store the large volumes of data being produced today.

SQL is a powerful tool required by anyone who deals with data on a day-to-day basis. Sql can insert data into a database.

It can search for some data in the database and also it supports updation and deletion of data stored in the database.

History of SQL

SQL was first brought into origin by IBM Researcher’s – Raymond F. Boyce, and Donald D. Chamberlin in the 1970’s and the initial version created by them was called SEQUEL or Structured English Query Language which worked on manipulation and retrieving data from IBM databases.

After commercial testing, IBM released various versions like System/38, SQL/DS, and DB2 in 1979, 1981, and 1983, respectively.

In 1986 making a breakthrough, ANSI and ISO adopted the Standard “Database Language SQL”.

Working of SQL

SQL works on commands and queries made to a Relational database. A relational database signifies that the data would be stored and retrieved in terms of relations or tables.

When we run on a command on SQL workbench or similar software, the command returns us the required results in the form of a table.

Next, we would learn what SQL commands and the types of SQL commands are.

What Can SQL Do?

We can use SQL for various operations. Some of them are stated below

1. We can use SQL to run queries on a database.
2. SQL is used to perform CRUD(Create, Retrieve, Update, Delete) operations on a database.
3. SQL is required to create and manage databases.
4. We use SQL to update and manipulate the existing data in the database.
5. SQL can be used to create views over an already existing database table.
6. SQL comes in handy when we need to divide permissions among different users of a database.
7. We are able to perform transactions easily on databases using SQL as it is compatible with almost all programming languages like – C++, Java, Python, etc.

Rules to Write SQL Query

We need to follow certain rules while writing a SQL query :

1. SQL queries are not case sensitive, but generally, we write SQL keywords in Uppercase for better understanding.
2. SQL statements can span in multi lines.
3. SQL follows the principle of tuple relational calculus and the rules of relational algebra.
4. SQL queries are capable of performing almost all actions on the database.

SQL Processes

1. When we run a SQL query on the Dbms or the RDBMS, the system looks into the request and tries to find the best possible way to execute it.
2. While a process is executed we need to take care of many components like – Query dispatcher and engine, optimization engine, etc.
3. SQL queries are not designed to handle the logical files; non SQL queries are interpreted by the classical query engine of sql.

Uses of SQL

Some of the uses of SQL are as follows:

1. Database Transaction Management: It is used to manage and configure the DBMS containing all sorts of data.

2. Reporting Purpose: SQL queries prove to be highly beneficial when it comes to reporting the facts and figures out of the bulk data stored in the relational Dbms.

3. Manual Analysis: SQL and its queries prove to be highly beneficial when we need to find problems in stored data, and human intervention is needed to correct the same.

4. Business Friendly: SQL provides many built-in aggregate and arithmetic functions which make it easy to use when dealing with large inflow of data and thus heavy calculations.

SQL Commands and Types

SQL Commands and Types

SQL works by running the commands provided by the user.

We can divide the SQL commands into five distinctions based on the functionality they provide:

1. DDL (Data Definition Language)

DDL is used to define the structure or schema of the database and to specify additional properties of the data.

These statements define the implementation of the schema and are mostly hidden from the user, also the data stored in the database is made to follow certain constraints.

Some of the DDL commands are as follows :

1. Create: to create objects in the database
2. Alter: alters the structure of the database
3. Drop: delete objects from the database
4. Rename: rename an object
5. Truncate: deletes all the data stored in a table

CREATE Command in SQL

Let us make a Student database here to study various available queries.

Syntax:

CREATE TABLE tableName ( columnName1 , datatype(length) , columnName2 , datatype(length), columnName3 , datatype(length));

Command to create Student database :

CREATE TABLE DataFlair_Employee (
name_emp  varchar(50),
post_emp varchar(50),
email varchar(50),
age int,
salary varchar(10)
);

SQL Introduction

Execution of the above stated DDL command creates the DataFlair_employee table consisting of four columns i.e. Name, Post, Email, Age and Salary.

2. DML(Data Manipulation Language)

DML is used for managing data contained in the schema objects. DML’s can be divided into two subtypes :

1. Procedural DML: In these types of DML’s we need to specify the type of data we need and how to get that data.
2. Declarative or Non-Procedural DML: Here, we only need to know what data is required, but the specifications on the process to get the data is not required. The database, on its own, figures out how to get the data efficiently.

Examples of DML Commands :

1. Insert: used to insert data in the database
2. Update: used to modify the existing data in the database
3. Delete: used to delete all the records contained (Note: The space used is not deleted)
4. Merge: used to merge two or more tables or rows.

Example :

a. Insert Command in SQL

Insert command is used to populate and add data to the database already existing. We can use the following

Syntax:

Insert into tableName (columnName1, columnName2, columnName3….) Values (value1,value2,value3,….) ;

Let us populate the database by using the insert command :
Insert into DataFlair_Employee (name_emp , post_emp , email , age , salary)

Insert into DataFlair_Employee (name_emp , post_emp , email , age , salary)
Values ('Ram' ,  "Intern", 'ram@dataflair.com', 21 , '10000' ),
('Shyam', "Manager", 'shyam@dataflair.com' , 25 , '25000'),
('Ria', "Analyst" , 'ram@dataflair.com', 23 , '20000'),
('Kavya', "Senior Analyst" , 'kavya@dataflair.com', 31 , '30000'),
('Aman', "Database Operator",' rish@dataflair.com' , 26 , '15000') ;

Create table in SQL

Executing this Insert statement populates our database and inserts the record of five employees in our database.

b. Update Command in SQL

The update command is used to change the data that is already saved into the database. This can be done by the following

Syntax:

UPDATE tableName SET column1 = value1, column2 = value2,… where condition;

Let us update the salary of Ram and change his age to 26 , the command is as follows :

Update dataflair_employee SET salary = 12000 , age = 26 where name_emp = 'Ria'  ;

Update command in SQL

c. Delete Command in SQL

Delete command is used to delete the existing records of a database. This can be done by the following

Syntax:

DELETE FROM table_name WHERE condition;

Let us delete the record corresponding to Kavya; we can get to this by the following command

DELETE from  dataflair_employee WHERE name_emp = 'Kavya' ;

delete command in SQL

3. DQL (Data Query Language)

DQl is also known as the Data Query language. It is used to retrieve the data stored in the database created by us and the data we store in the database.

a. Select Command in SQL

Let us now display the Student Table that we created.

Syntax:

Select * from tableName ;

after executing this command, we can view all the columns and all the data stored in the table.

Example:
a. SELECT * from DataFlair_employee : helps in viewing all the data i.e., all rows and columns.Select query in SQL

b. SELECT * from DataFlair_employee LIMIT 2; returns data contained in two rows of the database.

 

SQl Select query with Limit

c. Select name_emp , Age from DataFlair_employee : returns the data stored in the studentName and Age column of the Student database.

SQL Select query

These statements help in viewing the database in various aspects and different views.

4. TCL (Transaction Control Language)

TCL commands are used to handle transactions in the database.

These are used to manage and track the changes made by DML statements , these can be grouped together into logical transactions.

Example:
1. Commit: used to permanently save any transaction into the database.
2. Rollback: used to restore the database to the last committed state and is also used with the Savepoint command to move over to any saved state in a transaction.
3. Savepoint: It is used to temporarily store a transaction so that we can move to the point we need whenever desired.

5. DCL (Data Control Language)

DCL commands are similar to the computer programming language in syntax. They are a component of the Structured Query Language.

While using DCL commands, we need to be cautious as some database’s don’t allow rollback command on the DCL command.

One such example is the Oracle database – when we execute DCL on an oracle database it results in an implicit commit and thus rollback is not allowed.

Example :
1. Grant: used to allow a specific set of users to perform some specified tasks.
2. Revoke: used to cancel all the previously granted or denied permissions.

Summary

In this article, we have understood the basic introduction of what SQL is and how it is useful to us and where it is used.

We have studied the history of SQL and types of queries available in SQL i.e.

1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DQL – Data Query Language
4. TCL – Transaction Control Language
5. DCL – Data Control Language

We understood that SQL is a programming language used to handle and manage the activities related to a database i.e., the CRUD operations – Create, Retrieve, Update and Delete, and we have also discussed the commands related to the various operations.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google | Facebook

17 Responses

  1. Kartik Yadav says:

    what are the subqueries in SQL?

    • Data Flair says:

      Hi Kartik,
      I hope you read the complete SQL tutorial. Now you want to know about Subquery. So, here is the answer
      A query that is nested within any SELECT, INSERT, UPDATE, or DELETE statement is a subquery. It is also possible to nest a subquery in another. We can put a subquery anywhere we can put an expression.
      You can learn more about Subqueries fro the below link –
      https://data-flair.training/blogs/sql-subquery/

  2. Vishal Anand says:

    Hi Data Flair Team,

    This is really helpful and I will recommend Data Flair to others for learning SQL and other applications.

    Thank you,
    Vishal Anand

    • DataFlair Team says:

      Hello Vishal,
      Thanks for your kind words, we are glad you like our series of SQL Tutorials. Here, in DataFlair, you can found Tutorials on more than 40 technologies, which are popular and in-demand for 2019 like, Hadoop, Spark, Java, Python, R and many more. We recommend you to refer them as well.
      Regards,
      DataFlair

  3. Gitesh says:

    Awesome resource for getting sql tutorial. As per my thinking, there are TRUNCATE and RENAME commands exist in DDL. Kindly mention these both commands too.

    • DataFlair Team says:

      Thanks for suggestion Gitesh, we have separate SQL Tutorial for TRUNCATE command, please refer it.
      Reagrds,
      DataFlair

  4. Gallad says:

    Please send me this book

    • DataFlair Team says:

      Hello Gallad,
      DataFlair provides a free series of SQL Tutorials, which you can access and learn any time. We don’t provide any books, all the information is in the form of tutorials. We are providing more than 40 programming and technologies tutorials, you check out through our main menu.
      Hope, it helps

  5. omar hussien says:

    Hi Data Flair Team ,
    thank you all for your efforts
    It was a great benefit.
    And I’m going to keep up with you, one of the best sources I’m learning from.
    Omar

  6. Ahmad kamran says:

    Today only I started learning SQL from your website and makes a sense

    Kudos to the team!!!!

    Thanks & Regards
    Ahmad Kamran

  7. Ahmad kamran says:

    components fig in details pls

  8. Shawn gregory says:

    Hello DataFlair,
    i was going through your sql tutorials because they have lot of info that tutorials in other sites didnt have. But the Language used here is hard to understand and had to read atleast twice to get what the author wanted to say. Apart from that the tutorials have a lot of good info.

  9. Afis Adekola says:

    great effort from data flair team .

  10. girish says:

    Very good tutorials…Thansk awesom source for studies.

  11. K C says:

    thank u so much… the very good tutorial and understandable material

  12. Shriyash Mangaonkar says:

    Did not understood this point -> Allows entering among alternative languages exploitation SQL modules, libraries & pre-compilers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.