DEV Community

Rodney Kirui
Rodney Kirui

Posted on

Introduction to SQL

INTRODUCTION
SQL is one of the most common programming languages for interacting with data.

SQL consists of a data definition language, data manipulation language, and a data control language.

The data definition language deals with the schema creation and modification e.g., CREATE TABLE statement allows you to create a new table in the database and the ALTER TABLE statement changes the structure of an existing table.
The data manipulation language provides the constructs to query data such as the SELECT statement and to update the data such as INSERT, UPDATE and DELETE statements.
The data control language consists of the statements that deal with the user authorization and security such as GRANT and REVOKE statements.

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”.

RULES OF WRITING SQL QUERIES

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

SQL Commands and Types

1. DDL (Data Definition Language)
Deals with the schema creation and modification e.g., CREATE TABLE statement allows you to create a new table in the database and the ALTER TABLE statement changes the structure of an existing table. EXAMPLE;
CREATE TABLE DataFlair_Employee (
name_emp varchar(50),
post_emp varchar(50),
email varchar(50),
age int,
salary varchar(10)
);

2. DATA MANIPULATION LANGUAGE
provides the constructs to query data such as the SELECT statement and to update the data such as INSERT, UPDATE and DELETE statements.
Example;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') ;

3. DQL (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.

4. DCL (Data Control Language)
consists of the statements that deal with the user authorization and security such as GRANT and REVOKE statements.

Performing a simple calculation
The following example uses the SELECT statement to get the first name, last name, salary, and new salary:
SELECT
first_name,
last_name,
salary,
salary * 1.05
FROM
employees;

The expression salary * 1.05 adds 5% to the salary of every employee. By default, SQL uses the expression as the column heading:

To assign an expression or a column an alias, you specify the AS keyword followed by the column alias as follows:

expression AS column_alias
For example, the following SELECT statement uses the new_salary as the column alias for the salary * 1.05 expression:
SELECT
first_name,
last_name,
salary,
salary * 1.05 AS new_salary
FROM
employees;

CONCLUSION
In summary, SQL provides a standard syntax for interacting with relational databases, enabling users to easily retrieve, modify, and manage data in a variety of contexts.

Overall, the versatility and efficiency of SQL make it a critical component of modern data-driven applications, from business intelligence and data warehousing to web development and analytics.

Top comments (0)