What is SQL

What is SQL? SQL stands for Structured Query Language, a query language to access and update relational databases. In other words, if you need to read, insert, update, or delete data to a relational database, you will use SQL.

SQL and relational databases

 
Structured Query Language (SQL) is the language used in relational database management systems (RDBMS) to query, update, and delete data. SQL is a standard query language for RDBMS. SQL language’s queries are also known as SQL commands or SQL statements.
 

What is SQL

 
A relational database is DBMS that represent data in a tabular form of rows and columns. A table is a representation of an entity. A table is a combination of columns and rows. Each column in a table represents an attribute of the entity, also known as fields or properties. Each row in a table represents a record, the data associated with an entity. 
 
The following tabular data represents a Customer entity with attributes including ID, Company Name, Contact Name, Contact Title, Address, City, and so on. Each column of the table is an attribute (or property) of a Customer entity. Each row of the table represents a customer data.
 
Customers Table
 
Each column of a table has a data type that represents the kind of data a column can store. For example, the company name or customer name is a varchar type that can store characters, but a zip code is a numeric field and can store numbers only.
 
A SQL statement is the code that can be executed independently to generate some results. A SQL statement is a combination of queries that retrieve, update, or delete data based on specific criteria, clauses (the conditions), expressions, and predicates.
 
The following query is a complete SQL statement with clauses, expression, and predicate that selects ContactName and ContactTitle columns from the Customers table where ContactTitle has ‘Sales’ in it and City name is ‘London’.
 
SQL Statement 
 
Please note: 
  1. SQL is not a case sensitive language. That means, you can write in any case or combination of them.
  2. A complete SQL query can have multiple SQL statements in it.
  3. A semicolon is used to separate two SQL statements.
 
Here is a list of some of the most used SQL commands are 
  1. SELECT - Gets data from one or multiple database tables.
  2. UPDATE - Updates data in a database table.
  3. DELETE - Deletes data from a database table.
  4. INSERT INTO – Adds new data into a database table.
  5. CREATE DATABASE - Creates a new database.
  6. ALTER DATABASE - Modifies an existing database.
  7. CREATE TABLE - Creates a new table.
  8. ALTER TABLE - Modifies an existing table.
  9. DROP TABLE - Deletes an existing table.
Here are more details on these commands with code samples: SQL Server Interview Questions

SQL as Data Query Language

 
SQL DQL (Data Query Language) command allows us to query and retrieve data from a database.
 
SQL SELECT command is a DQL command. The SELECT command is a combination of SELECT, FROM, and WHERE clauses.
 
The SELECT clause is followed by a comma separated list of the column names. The FROM clause is followed by one or more table names, and the WHERE clause is a condition to filter records.
  1. SELECT column1, column2, column3  
  2. FROM table_name;  
  3. WHERE condition  
Here is a simple SELECT command that selects all records from the table, Customers. 
  1. SELECT *  
  2. FROM Customers  
The following SELECT command selects three columns from the Customers table in London city. 
  1. SELECT ContactName, ContactTitle, City  
  2. FROM Customers  
  3. WHERE City='London'  

SQL as Data Definition Language

 
SQL DDL (Data Definition Language) commands are used to create and manipulate database objects including database itself, tables, and other objects. Most commonly used DDL commands are CREATE, ALTER and DROP.
 
Here are the examples of CREATE, ALTER, and DROP commands.
 
CREATE DATABASE command creates a new database.
  1. CREATE DATABASE databasename;  
  2. CREATE TABLE creates a new database table.  
  3. CREATE TABLE tablename (  
  4. column1 datatype,  
  5. column2 datatype,  
  6. column3 datatype,  
  7. );  
The following CREATE TABLE command creates a new table with 3 columns.
  1. CREATE TABLE Authors (  
  2. AuthorID int,  
  3. Name varchar(255),  
  4. Book varchar(255),  
  5. Publisher varchar(255)
  6. );  
ALTER TABLE command alters a database table schema. For example, if you want to change the data type of a column or want to add a new column.
 
ALTER TABLE tablename
ADD columnname datatype;
 
The following command alters Authors table by adding a new column to the table. 
  1. ALTER TABLE Authors  
  2. ADD Email varchar(255);  
DROP DATABASE command deletes a database.
 
DROP DATABASE databasename;
 
DROP TABLE deletes a database table.
 
DROP TABLE tablename;
 
The following command deletes Authors table. 
  1. DROP TABLE Authors;  

SQL as Data Manipulation Language

 
SQL provides DML (Data Manipulation Language) commands that are used insert, update and delete data. Common DML commands are INSERT, UPDATE and DELETE.
 
Here are some of the examples of INSERT INTO, UPDATE, and DELETE commands.
 
INSERT INTO command inserts new records to a database table.
 
  1. INSERT INTO TableName (Column1, Column2, Column3)  
  2. VALUES (value1, value2, value3);  
The following SQL command inserts one record. 
  1. INSERT INTO Authors (AuthorID, Name, Book, Publisher)  
  2. VALUES (1001, 'Mahesh Chand''Programming ADO.NET''APress');  
UPDATE command updates existing records. The following UPDATE SQL command updates an existing record where AuthorID = 1001. 
  1. UPDATE Authors  
  2. SET AuthorID = 100, Book ='C# 8'  
  3. WHERE AuthorID = 1001  
DELETE command deletes existing records. The following DELETE command deletes existing records where AuthorID is 100. 
  1. DELETE FROM Authors WHERE AuthorID = 100  

SQL as Data Control Language

 
SQL DCL (Data Control Language) commands are used to control the access on a database includes privileges on database objects. Common DCL commands are GRANT and REVOKE.
 

SQL History

 
SQL was first introduced at IBM in 1970 by Donald D. Chamberlin and Raymond F. Boyce for Edgar F. Codd's relational model. It became a standard of the American National Standards Institute (ANSI) in 1986. Today, SQL is the most used database programming language in the world and used in most of the popular relational databases including MySQL, SQL Server, Oracle, SQLite, and DB2.
 
SQL is a cross-platform, static, h2 typed, multi-paradigm, declarative computer programming language. SQL files are stored as .sql files and can be executed from a command line or using various UI tools.
 

Learn SQL

 
Here are two free eBooks downloads (PDF) on SQL queries for beginners:
 
SQL Queries Book     SQL Book
 
Here are some resources to learn SQL programming.
 
 
If you’ve 4 hours to spend, what this compete SQL tutorial, Full Course for Beginners in 4 hours.
 
 

Summary

 
In this article, we learn what SQL is all about, its types and how SQL commands are created and used in relational database management systems.
 
Here is more on SQL: SQL Interview Questions 
 

References

 
https://en.wikipedia.org/wiki/SQL