What Is SQL And How To Learn SQL

Introduction

 
SQL is a query language for relational databases that allows data definition, data access, and data manipulation functionality. In other words, if you need to read, insert, update, or delete data to a relational database, you will use SQL.
 
SQL is the most popular query language and used to access and manipulate relational databases such as SQL Server, Oracle, MySQL, and DB2.
 

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.
 
 
A relational database is a DBMS that represents 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.
 
 
Each the 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 a code that can be executed independently to generate some results. A SQL statement is a combination of queries that retrieves, updates, or deletes data based on specific criteria, clauses (the conditions), expressions, and predicates.
 
The the following query is a complete SQL statement with clauses, expression, and a predicate that selects ContactName and ContactTitle columns from the Customers table where ContactTitle has ‘Sales’ in it and the City name is ‘London’.
 
 
Please note:
  • SQLis not a case sensitive language. That means you can write in any case orcombination of them.
  • A complete SQL query can have multiple SQL statements in it.
  • Asemicolon 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. INSERTINTO- Adds new data into a database table.
  5. CREATEDATABASE- Creates a new database.
  6. ALTERDATABASE- Modifies an existing database.
  7. CREATETABLE- Creates a new table.
  8. ALTERTABLE- Modifies an existing table.
  9. DROPTABLE- Deletes an existing table.

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' 
The result of the command looks like the following:
 
 

SQL as Data Definition Language

 
SQL DDL (Data Definition Language) commands are used to create and manipulate database objects including the database itself, tables, and other objects. Most commonly used DDL commands are CREATE, ALTER, and DROP.
 
Here are examples of CREATE, ALTER, and DROP commands.
  1. CREATE DATABASE command creates a new database.  
  2.    
  3. CREATE DATABASE databasename;  
  4.    
  5. CREATE TABLE creates a new database table.  
  6.    
  7. CREATE TABLE tablename (  
  8.     column1 datatype,  
  9.     column2 datatype,  
  10.     column3 datatype,  
  11. ); 
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.
  1. ALTER TABLE tablename  
  2. ADD columnname datatype; 
The following command alters the 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.
  1. DROP DATABASE databasename; 
DROP TABLE deletes a database table.
  1. 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 to 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 the command deletes existing records. The following DELETE command deletes existing records where AuthorID is 100.
 
DELETE FROM Authors WHERE AuthorID = 100
 

SQL as Data Control Language

 
SQL DCL (Data Control Language) commands are used to control access on a database includes privileges on database objects. Common DCL commands are GRANT and REVOKE.
 
Here is a detailed tutorial on how to use SQL GRANT and SQL REVOKE statements.
 

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 is used in most of the popular relational databases including MySQL, SQL Server, Oracle, SQLite, and DB2.
 
SQL is a cross-platform, static, strong 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

 
If you have four hours to spend, watch this compete SQL tutorial, Full Course for Beginners in 4 hours.
  1. <iframe width="560" height="315" src="https://www.youtube.com/embed/HXV3zeQKqGY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> 
     
Here are two free eBooks downloads (PDF) on SQL queries for beginners:
 
https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/EBooks/06242016065531AM/06242016070514AMbook-small.png
 
https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/EBooks/11262014030522AM/11262014030814AMsql_query_Book_small.png
Here are some videos on SQL for beginners.
 
T-SQL Tutorials
 

Summary

 
In this article, we learned what SQL is all about, its types and how SQL commands are created and used in relational database management systems.
 
References
  1. https://en.wikipedia.org/wiki/SQL