Introduction to SQL

Jeetika Khandelwal
Analytics Vidhya
Published in
4 min readAug 14, 2020

SQL stands for Structured Query Language. SQL lets you access and manipulate databases. SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987.

What can SQL do?

  • SQL can execute queries against a database. It can retrieve data from a database
  • SQL can insert, update, delete records in a database.
  • SQL can create new tables in a database
  • SQL can create stored procedures in a database
  • SQL can create views in a database
  • SQL can set permissions on tables, procedures, and views
  • SQL can join various tables.

Why do we use SQL over Excel?

SQL has many clear advantages over Excel:

  • Excel finds it difficult to deal with large data sets.
  • There is a higher possibility of making accidental changes in data using Excel. On other hand in SQL one needs to code and understand before making any chances to their data.
  • SQL can save your time. It is time efficient.
  • SQL makes editing data very easy.

SQL Commands

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into the following groups based on their nature −

DDL- Data Definition Language

  1. CREATE- Creates a new table, a view of a table, or other object in the database.
  2. ALTER- Modifies an existing database object, such as a table.
  3. DROP- Deletes an entire table, a view of a table or other objects in the database.

DML -Data Manipulation Language

  1. INSERT- Creates a record.
  2. UPDATE- Modifies records.
  3. DELETE- Deletes records.

DCL- Data Control Language

  1. GRANT- Gives a privilege to user.
  2. REVOKE- Takes back privileges granted from user

TCL — Transaction Control Language

  1. COMMIT: saves any changes made to the database.
  2. ROLLBACK: undoes any changes made to the database.
  3. SAVEPOINT: creates a point in your transaction to which you can roll back to.

Let us begin learning few basics but very important statements.

The SQL SELECT Statement

The SELECT statement is used to select data from a database.The data returned is stored in a result table.

SELECT column1, column2, …
FROM table_name;

This selects specific columns that users needs. To select everything use *

SELECT * FROM table_name;

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values and sometimes you only want to list the different (distinct) values.

SELECT DISTINCT column1, column2, …
FROM table_name;

The SQL WHERE Clause

The WHERE clause is used to filter records.

SELECT column1, column2, …
FROM table_name
WHERE condition;

The WHERE clause can be combined with AND, OR, and NOT operators.

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);

OR

INSERT INTO table_name
VALUES (value1, value2, value3, …);

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;

The SQL DELETE Statement

The DELETE statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition;

SQL Aggregate Functions

The SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

  • % — The percent sign represents zero, one, or multiple characters
  • _ — The underscore represents a single character

The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country”.

The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

So here we complete all the basic concepts of SQL.

NOTE : There are various advance SQL topics yet to be covered. This is just to give a brief idea to the once who wants to know basics of SQL.

References : https://www.w3schools.com , https://2.bp.blogspot.com , https://www.cdn.geeksforgeeks.org

--

--

Jeetika Khandelwal
Analytics Vidhya

Believer of data driven decisions who knows how data can be brought into play to solve business related problems