Structured Query Language: Introduction To
Structured Query Language: Introduction To
Structured
Query
Language
Introduction to Structured Query Language
SQL (Structured Query Language) is a standardized language that is used to manage and
manipulate relational databases. It is used to create, modify, and query databases, as well as
perform various other operations on the data stored in the databases.
SQL was first introduced in the 1970s and has since become the most widely used language for
managing relational databases. It is used by many businesses, organizations, and individuals
for a wide range of applications, including data warehousing, e-commerce, financial
management, and more.
SQL is a declarative language, meaning that we specify what we want to do with the data, and
the database management system (DBMS) takes care of the details of how to perform the
desired operation. This makes SQL a very efficient and effective language for working with large
amounts of data.
Table of Contents
● What is SQL
● Why Learn Structured Query Language (SQL)?
● SQL Applications
● Installing Mysql
● Data Types and Constraints in MySQL
● SQL for Data Definition
● SQL for Data Manipulation
● SQL for Data Query
● Data Updation and Deletion
Structured Query Language (SQL) is a standard programming language that is used to manage
and manipulate relational databases. It is used to create, modify, and query databases, as well
as to perform various other operations on the data stored in the databases. SQL is used by a
wide range of applications and is widely recognized as an industry standard for working with
relational databases.
With SQL, we can perform operations such as creating tables, inserting data into tables,
updating data, retrieving data based on certain conditions, and deleting data. It provides a rich
set of commands and functions for working with databases, and its simple syntax makes it easy
to learn and use.
Why Learn Structured Query Language (SQL)?
There are several reasons why learning Structured Query Language (SQL) is important:
● Widely used: SQL is the most widely used language for managing relational databases,
and it is used by a large number of organizations, businesses, and individuals. Learning
SQL will give a valuable skill that is in high demand.
● Essential for data analysis: In today's data-driven world, to work with large amounts of
data is becoming increasingly important. SQL is a powerful tool for retrieving and
analyzing data, and it is essential for anyone who wants to work with data in a
meaningful way.
● Easy to learn: SQL has a relatively simple syntax and is easy to learn. With a basic
understanding of SQL, we can be up and running with our first database project in no
time.
● Versatile: SQL can be used for a wide range of tasks, including creating and modifying
databases, inserting, updating, and retrieving data, and more. It can be used to work
with small databases or large data warehouses, making it a versatile tool for a wide
range of use cases.
● In demand: With the increasing importance of data, the demand for people with SQL
skills is growing. Learning SQL can open up new career opportunities, or it can be a
valuable addition to our current skillset.
In general, learning SQL is a valuable investment for future, as it is an essential tool for working
with data and is in high demand across a wide range of industries.
SQL Applications
SQL (Structured Query Language) is a widely used language for managing relational databases
and is used in many different applications and industries. Some of the most common
applications of SQL include:
1. Data Warehousing: SQL is commonly used to manage and analyze huge amounts of
data stored in data warehouses. This allows organizations to store, retrieve, and analyze
large amounts of data in an efficient and effective manner.
2. E-Commerce: SQL is used to manage and manipulate the data stored in databases for
e-commerce websites. This includes managing product information, customer
information, and order data.
3. Financial Management: SQL is used by financial institutions to manage and analyze
large amounts of financial data. This includes tracking transactions, generating reports,
and analyzing financial trends.
4. Marketing: SQL is used by marketing departments to analyze customer data and
understand consumer behavior. This information can be used to develop more effective
marketing campaigns and improve overall customer satisfaction.
5. Healthcare: SQL is used to manage and analyze healthcare data, such as patient
information, medical records, and prescription data. This information can be used to
improve patient care and streamline healthcare processes.
These are just a few examples among the many applications of SQL. The versatility and power
of SQL make it a important tool in a wide range of industries, and its use continues to grow as
the importance of data management and analysis continues to increase.
Then click on Local instance we will be directed to the MySQL workbench as shown below.
A few guidelines to remember when developing SQL statements in MySQL:
• SQL does not case sensitive. That is, SQL's name and NAME are the same.
• Always use a semicolon at the end of SQL statements (;).
• We do not use ';' after the first line when entering multiline SQL statements. We pressed enter
to move on to the next line. Activity 8.1 changes the prompt mysql> to '->'.
Investigate LibreOffice Base and contrast it with MySQL 2022-23.
Structured Query Language (SQL) 145 indicates that the statement is continued to the following
line. Put ';' after the final line and press enter.
We need to understand that a database is made up of one or more relations, and that each
relation (table) is made up of attributes (column). Each attribute is associated with a data type.
We may also specify limitations for each relation attribute.
The data type of an attribute indicates the sort of data value that it can have. The operations
that can be done on an attribute's data are determined by its data type. Arithmetic operations,
for example, can be performed on numeric data but not on character data. Numeric data types,
string (character and byte) data types and date and time data types, are all often used in
MySQL.
Constraints are rules or conditions that are imposed on the data within a database to ensure
data integrity and accuracy. They are used to specify the relationships between tables, limit the
type of data that can be entered into a table, and ensure that data entered into a database is
consistent and meets certain standards.
Constraint Description
PRIMARY KEY Column that can uniquely identify each row/record in a table.
These constraints help maintain data consistency and accuracy within a database, improving
the reliability and stability of the data stored in the database.
SQL commands are available for defining relation schemas, changing relation schemas, and
removing relations. These are known as Data Definition Language (DDL), and they specify the
collection of relations, including their schema, data type for each attribute, restrictions, and
security and access authorizations.
The create statement is the first step in the data definition process. This statement is used to
build a database and the tables within it (relations). Before constructing a database, we should
understand the number of tables, the columns (attributes) in each table, and the data type of
each column. This is how the relation schema is determined.
1.Create Database:
To create a database in MySQL, use the CREATE DATABASE statement. For example, to
create a database named databasename, can use the following command:
syntax: CREATE DATABASE databasename;
USE EmployeeData
The new database is initially empty. It can be tested by using the Show tables command, which
displays the names of all the tables in a database.
Syntax : SHOW TABLES
2. Create Table
After constructing the database StudentAttendance, we must define relations (make tables) and
provide attributes for each relation, as well as data types for each attribute. The CREATE
TABLE statement is used to accomplish this.
Syntax: CREATE TABLE tablename( attributename1 datatype constraint,
attributename2 datatype constraint,
.
.
.
attributenameN datatype constraint);
It is important to keep the following points in mind when using the Create Table statement:
• N is the degree of the relation, which means the table has N columns.
• Attribute name defines the name of the table column.
• Datatype determines the type of data that can be stored in an attribute.
• Constraint denotes the limitations placed on the values of an attribute. Except for the primary
key, each attribute can have NULL values by default.
Example:
Note: ‘,’ is used to separate two attributes and each statement terminates with a semi-colon (;).
The symbol ‘->’ indicates line continuation as SQL statement may not complete in a single line.
3. DESCRIBE Table
Using the describe statement, we can see the structure of a previously built table.
Syntax :DESCRIBE tablename;
MySQL also offers the short version DESC of DESCRIBE to acquire table description.
Example:
After building a table, we may discover that we need to add/remove an attribute, change the
datatype of an existing attribute, or apply a constraint to the attribute. In all of these
circumstances, we must use the alter statement to edit or alter the structure of the table.
Syntax: ALTER TABLE tablename ADD/Modify/DROP attribute1, attribute2,..
We can see that a new column named address is added to the employees table with a data type
of VARCHAR(100). We can add additional constraints, such as NOT NULL or UNIQUE, as
necessary.
c.) Modify the datatype of an attribute
We can modify data types of the existing attributes of a table using the following ALTER
statement.
Syntax: ALTER TABLE table_name MODIFY attribute DATATYPE;
For example, we want to change the data type of the salary column
ALTER TABLE employees
MODIFY salary int;
5.DROP Statement
Sometimes a database table or the database itself must be removed. We can use the DROP
statement to permanently remove a database or a table from the system. However, because
this phrase cannot be reversed, it should be used with extreme caution.
To delete a table, syntax DROP TABLE table name;
To delete a database, syntax DROP DATABASE database name.
Cautions:
1) Using the Drop statement to remove a database will eventually erase all of its tables.
2) The DROP statement will delete any tables or databases that we have built. As a result, at
the end of the guide, we can use the DROP statement.
We constructed the database employeedata in the previous part, which has two
relations:Employees and Departments. When we construct a table, we just create its structure;
the table has no data.The INSERT statement is used to populate records in the table. Similarly,
SQL data manipulation statements can be used to delete or alter table records.
Using a database to manipulate data implies retrieving (accessing) existing data, inserting new
data, removing existing data, or modifying existing data in the database.
a.).INSERTION of Records
To insert new records into a table, use the INSERT INTO statement. It has the following
syntax: INSERT INTO tablename VALUES(value 1, value 2,....);
where value 1 corresponds to attribute 1, value 2 corresponds to attribute 2, and so on.
It is important to note that we do not need to give attribute names in the insert statement if the
number of values in the INSERT statement equals the entire number of attributes in the table.
Caution: Before filling records in a table with a foreign key, check to see if entries in linked tables
are already populated.
Let us insert some records in the employeedata database. We shall insert records in the
Departments table first as it does not have any foreign key.
So far, we've learned how to make a database, as well as how to store and alter data. We want
to save data in a database because it will make it easier to access data from databases in the
future in any form we desire.
The Structured Query Language (SQL) includes tools for retrieving data from many tables in a
MySQL database (or any other RDBMS). The user enters SQL commands known as queries,
which specify the specific data requirements to be retrieved. The SQL statement SELECT,
commonly known as a query statement, is used to retrieve data from tables in a database.
a.) SELECT statement
The SQL statement SELECT is used to get data from database tables, and the results are also
shown in tabular form.
Syntax:
SELECT attribute1, attribute2,...
FROM table_name.
WHERE condition;
The column names of the table table_name from which we want to get data are represented by
attribute1, attribute2,... The FROM clause is always followed by the SELECT clause since it
indicates the name of the table from which data will be fetched. The WHERE clause is optional
and is used to retrieve data that matches a set of criteria (s).
We will create a database for demonstration and perform different queries on the database
Code:
CREATE DATABASE OFFICE
USE OFFICE
Now we will create three tables and insert records into the database
SELECT name
FROM departments;
2.DISTINCT Clause
SQL outputs all of the data returned by the query by default. However, duplicate values are
possible.When coupled with DISTINCT, the SELECT statement clause, returns records that do
not repeat (distinct records). When accessing an employee's department number, for example,
there may be duplicate values since multiple employees are allocated to the same department.
We use DISTINCT to display a unique department number for each employee, as illustrated
below:
Join: The JOIN clause is used in SQL to combine rows from two or more tables based on a
related column between them. The related column is called a join condition, and it's used to
match rows from one table with rows from another table.
Where: The WHERE clause is used to get datal that meets certain criteria. More than one
employee can have the same pay in the OFFICE database.
We will understand this better with the following example
To display distinct wages of employees working in the department number 1, we build the
following query in which the WHERE clause is used to select the employee whose department
number is 1.
Data Updation and Deletion are two important operations in SQL for managing database data.
Data Updation refers to the process of modifying existing data in a database. This can be done
using the UPDATE statement in SQL. The UPDATE statement allows to modify specific columns
in a table, or all columns, based on a specific condition. For example, update the salary of an
employee based on their id, or update the email address of all employees in a certain
department.
Data Deletion refers to the process of removing data from a database. This can be done using
the DELETE statement in SQL. The DELETE statement allows to remove rows from a table
based on a specific condition. For example, we could delete all employees who have left the
company, or remove all records of a certain product that is no longer available.
It's important to be cautious when updating or deleting data, as these operations can have a
significant impact on the integrity and accuracy of our database. Before making any changes,
it's always a good idea to backup our data and make sure we understand the consequences of
our actions. Deleted data cannot be recovered, so make sure there is a plan in place to restore
our database in the event of an error or unintended outcome.
Here's an example of how to update data in the employees table:
This query increases the salary of the employee with the id of 1 by 1000.
This query deletes the record where name = ‘HR’ from the departments table
Note: When deleting data, it's important to be cautious and always make a backup of our data
before making any changes. Deleted data cannot be recovered.
Conclusion :
SQL is an essential tool for working with relational databases, and its importance will only
continue to grow as more and more organizations rely on data-driven insights to inform their
decision-making. Whether a data professional, software developer, or simply someone looking
to manage and analyze data, learning SQL is an investment that is well worth the time and
effort.
If you're looking to enter the Data Science
field, then AlmaBetter is the best place to
start your journey.
Link: https://link.almabetter.com/3yuy