top of page
Writer's pictureSofia Sondh

What is SQL and How it works?

SQL (Structured Query Language) is a programming language used to manage and manipulate data in relational databases. It is used to insert, update, retrieve and delete data in a database. SQL is a powerful tool for managing and manipulating data, but it can be complex to use, and it requires a certain level of knowledge and understanding of relational databases.




How SQL Works?

SQL works by allowing users to write commands (also known as queries) that are then executed by the database management system (DBMS) to perform specific tasks. The DBMS interprets the SQL commands and communicates with the database to carry out the requested operations.


Here are the basic operations that SQL can perform:

  1. SELECT - used to retrieve data from the database.

  2. INSERT - used to insert new data into the database.

  3. UPDATE - used to modify existing data in the database.

  4. DELETE - used to delete data from the database.

  5. CREATE - used to create new tables, databases or other database objects.

  6. ALTER - used to modify the structure of a table or other database object.

  7. DROP - used to delete tables or other database objects.


1. SELECT Command

The basic syntax of a SELECT statement in SQL is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE conditionGROUP BY column1, column2, ...
HAVING conditionORDER BY column1, column2, ... [ASC|DESC];
  • The SELECT clause is used to specify the columns that you want to retrieve from the table. You can specify one or more columns, separated by commas.

  • The FROM clause is used to specify the table from which you want to retrieve the data.

  • The WHERE clause is used to filter the rows based on one or more conditions.

  • The GROUP BY clause is used to group the rows based on one or more columns.

  • The HAVING clause is used to filter the groups based on one or more conditions.

  • The ORDER BY clause is used to sort the result set based on one or more columns in ascending or descending order.

Here is an example:

SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000GROUP BY department
HAVING COUNT(*) > 5ORDER BY last_name DESC;

This query selects the first name, last name and salary of employees whose salary is greater than 50000, group them by department, filter the groups which have count of employees greater than 5 and then sort the result set based on last name in descending order.


2. INSERT Command

The basic syntax of an INSERT statement in SQL is as follows:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
  • The INSERT INTO clause is used to specify the table that you want to insert data into.

  • The (column1, column2, ...) is an optional list of columns that you want to insert data into. If you don't specify the column names, the values must be in the same order as the table's columns.

  • The VALUES clause is used to specify the values that you want to insert into the table. The values must be in the same order as the column names, or in the same order as the table's columns if you didn't specify column names.

Here is an example:

INSERT INTO employees (first_name, last_name, salary)
VALUES ('John', 'Doe', 70000);

This query insert a new row into the "employees" table, with values 'John' for the column "first_name", 'Doe' for the column "last_name" and 70000 for the column "salary".

You can also use the SELECT statement to insert data into a table by specifying the SELECT statement after the VALUES clause.

INSERT INTO employees (first_name, last_name, salary)
SELECT first_name, last_name, salary
FROM temp_employees
WHERE department = 'IT';

This query insert rows into the "employees" table that are selected from the "temp_employees" table where department is 'IT


3. UPDATE Command

The basic syntax of an UPDATE statement in SQL is as follows:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  • The UPDATE clause is used to specify the table that you want to update.

  • The SET clause is used to specify the columns and their new values that you want to update. You can update one or more columns, separated by commas.

  • The WHERE clause is used to specify the condition that identifies the rows that you want to update. If you don't specify a condition, all rows in the table will be updated.

Here is an example:

UPDATE employees
SET salary = salary + 5000, department = 'IT'WHERE last_name = 'Doe';

This query updates the salary of all employees whose last name is 'Doe' by adding 5000 and also updates their department to 'IT'.


4. DELETE Command

The basic syntax of a DELETE statement in SQL is as follows:

DELETE FROM table_name
WHERE condition;
  • The DELETE FROM clause is used to specify the table that you want to delete data from.

  • The WHERE clause is used to specify the condition that identifies the rows that you want to delete. If you don't specify a condition, all rows in the table will be deleted.

Here is an example:

DELETE FROM employees
WHERE last_name = 'Doe' AND salary < 50000;

This query deletes all rows from the "employees" table where last name is 'Doe' and salary is less than 50000.


5. CREATE Command

The basic syntax for creating a new table in SQL is as follows:

CREATE TABLE table_name (
  column1 data_type constraint,
  column2 data_type constraint,
  ...
  constraint
);
  • The CREATE TABLE clause is used to create a new table.

  • The table_name is the name of the table you want to create.

  • The column1, column2, ... are the column names of the table and data_type is the data type of the columns like INT, VARCHAR, CHAR, DATE, etc.

  • The constraint is used to specify the constraints on the columns like NOT NULL, UNIQUE, PRIMARY KEY, etc.

Here is an example:

CREATE TABLE employees (
  id INT PRIMARY KEY,
  first_name VARCHAR(255) NOT NULL,
  last_name VARCHAR(255) NOT NULL,
  salary DECIMAL(10, 2) NOT NULL,
  department VARCHAR(255),
  UNIQUE (first_name, last_name)
);

This query creates a new table named "employees" with columns "id", "first_name", "last_name", "salary" and "department". The column "id" is set as the primary key and "first_name" and "last_name" are set as unique.


6. ALTER Command

The basic syntax for ALTERING a table in SQL is as follows:

ALTER TABLE table_name 
{ADD | DROP | MODIFY} 
{column_name data_type constraint | constraint_name | INDEX index_name | TRIGGER trigger_name}
  • The ALTER TABLE clause is used to modify the structure of an existing table.

  • The table_name is the name of the table you want to modify.

  • The ADD, DROP or MODIFY keyword is used to specify the type of modification you want to make.

  • ADD is used to add a new column to the table or add a new constraint to an existing column.

  • DROP is used to delete a column from the table or remove a constraint from an existing column.

  • MODIFY is used to modify the data type of a column, or to change a constraint on a column.

  • column_name is the name of the column you want to add, drop or modify.

  • data_type is the data type of the column you want to add or modify.

  • constraint is used to specify the constraints on the columns like NOT NULL, UNIQUE, PRIMARY KEY, etc.

  • constraint_name is the name of the constraint you want to drop or modify.

  • INDEX index_name is used to add or drop an index on the table.

  • TRIGGER trigger_name is used to add or drop a trigger on the table.

Here is an example:

ALTER TABLE employees
ADD email VARCHAR(255);

This query adds a new column named "email" with data type VARCHAR(255) to the "employees" table.


Here is another example:

ALTER TABLE employees
DROP COLUMN department;

This query removes the column named "department" from the "employees" table.


7. DROP Command

The basic syntax for dropping a table, index, or other database object in SQL is as follows:

DROP {TABLE | INDEX | TRIGGER | ...} [IF EXISTS] name;
  • The DROP clause is used to delete an existing table, index, trigger, or other database object.

  • The {TABLE | INDEX | TRIGGER | ...} specifies the type of object you want to delete.

  • The [IF EXISTS] is an optional clause that allows you to check if the object exists before dropping it, so it will not raise an error if the object does not exist.

  • The name is the name of the table, index, trigger, or other object that you want to delete.

Here is an example:

DROP TABLE employees;

This query deletes the "employees" table.

Here is another example:

DROP INDEX idx_employees_last_name;

This query deletes the index named "idx_employees_last_name"


SQL Working

SQL queries are usually executed on a relational database, which is a type of database that organizes data into one or more tables with rows and columns. Each table represents a different entity, and the rows represent individual instances of that entity. The columns represent the attributes of the entity.



Here's a general overview of how SQL works:


1. User Write SQL Query

A user writes an SQL query, which is a command that tells the DBMS what operation to perform on the database. The query is written in a specific syntax, which is a set of rules that define how the query should be written.


2. Query is submitted to DBMS

The user submits the query to the DBMS, which interprets the query and checks it for errors.


3. Storage and Optimizing the Data

The DBMS then communicates with the database where the data is stored. The database is organized into tables, which are collections of related data. Each table has rows and columns, where each row represents a single record of data and each column represents a specific field or attribute of that data.


4. Request Operation

The DBMS then performs the requested operation on the database. For example, if the query is a SELECT statement, the DBMS will retrieve the specified data from the database and return it to the user. If the query is an INSERT statement, the DBMS will add the specified data to the database.


5. DBMS sent result back to user

Once the operation is complete, the DBMS sends the results back to the user. The results can be displayed in a variety of formats, such as a table or a list.


Note: Different DBMS have different implementation methods but the overall working remains the same.


SQL provides a flexible and powerful way to manage and manipulate data in a relational database, but it also requires a certain level of knowledge and understanding of the underlying database structure and the SQL language itself.


Advantages of SQL:

  1. It is a standard language for managing and manipulating relational databases.

  2. It is easy to learn and use.

  3. It is a declarative language, which means that users only need to specify what they want the database to do, not how to do it.

  4. It is highly expressive and can be used to perform a wide variety of tasks, including data retrieval, insertion, updates, and deletion.

  5. It is highly portable, meaning that SQL code can be easily ported from one database management system to another.

Disadvantages of SQL:

  1. It can be slow for very large databases.

  2. It can be difficult to use for certain types of tasks, such as complex data analysis or machine learning.

  3. It is not always the most efficient way to retrieve or manipulate data, particularly when working with unstructured or semi-structured data.

  4. It is not a programming language and thus is not suitable for all the operations that a programming language can do.

  5. It can be vulnerable to SQL injection attacks if not properly secured.



The Tech Platform

0 comments

Commentaires


bottom of page