Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Structured Query Language

Uploaded by

jubairahmed1678
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Structured Query Language

Uploaded by

jubairahmed1678
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Structured Query Language

SQL
What is SQL?

• SQL is a short-form of the structured query language, and it is


pronounced as S-Q-L or sometimes as See-Quell.
• This database language is mainly designed for maintaining the data in
relational database management systems. It is a special tool used by
data professionals for handling structured data (data which is stored in
the form of tables).
• You can easily create and manipulate the database, access and modify
the table rows and columns, etc. This query language became the
standard of ANSI in the year of 1986 and ISO in the year of 1987.
Why SQL?
Nowadays, SQL is widely used in data science and analytics. Following are the reasons
which explain why it is widely used:
• The basic use of SQL for data professionals and SQL users is to insert, update, and delete
the data from the relational database.
• SQL allows the data professionals and users to retrieve the data from the relational
database management systems.
• It also helps them to describe the structured data.
• It allows SQL users to create, drop, and manipulate the database and its tables.
• It also helps in creating the view, stored procedure, and functions in the relational
database.
• It allows you to define the data and modify that stored data in the relational database.
• It also allows SQL users to set the permissions or constraints on table columns, views,
and stored procedures.
Applications of SQL

Execute different database queries against a database.


Define the data in a database and manipulate that data.
Create data in a relational database management system.
Access data from the relational database management system.
Create and drop databases and tables.
Create and maintain database users.
Create view, stored procedure, functions in a database.
Set permissions on tables, procedures and views.
Various Syntax in SQL
SQL SELECT Statement
SELECT column1, column2....columnN
FROM table_name;
SQL WHERE Clause
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;
SQL AND/OR Clause
SELECT column1, column2....columnN
FROM table_name
Various Syntax in SQL

SQL IN Clause
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...valN);
SQL BETWEEN Clause
SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;
SQL LIKE Clause
SELECT column1, column2....columnN
FROM table_name
Various Syntax in SQL
SQL ORDER BY Clause
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};
SQL GROUP BY Clause
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;
SQL COUNT Clause
SELECT COUNT(column_name)
FROM table_name
Various Syntax in SQL
SQL HAVING Clause
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithmetic function condition);

SQL CREATE TABLE Statement


CREATE TABLE table_name(
column1 datatype,
column2 datatype,
Various Syntax in SQL

SQL DROP TABLE Statement


DROP TABLE table_name;

SQL DESC Statement


DESC table_name;

SQL INSERT INTO Statement


INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
Data Types in SQL - Numeric
Data Type From To
BIT 1 0

TINYINT 0 255

SMALLINT -32,768 32,767

INT -2,147,483,648 2,147,483,647

BIGINT -9,223,372,036,854,775,808 9,223,372,036,854,775,807

DECIMAL -10^38 + 1 10^38 - 1

NUMERIC -10^38 + 1 10^38 - 1

FLOAT -1.79E+308 1.79E+308

REAL -3.40E+38 3.40E+38


Data Types in SQL – Date & Time

DATE Stores date in the format YYYY-MM-DD

TIME Stores time in the format HH:MI:SS

DATETIME Stores date and time information in the format YYYY-MM-DD


HH:MI:SS

TIMESTAMP Stores number of seconds passed since the Unix epoch ('1970-01-
01 00:00:00' UTC)

YEAR Stores year in a 2-digit or 4-digit format. Range 1901 to 2155 in 4-


digit format. Range 70 to 69, representing 1970 to 2069.
Data Types in SQL – Character & String
Data Type Description

CHAR Fixed length with a maximum length of 8,000 characters

VARCHAR Variable-length storage with a maximum length of 8,000


characters

Variable-length storage with provided max characters, not


VARCHAR(max) supported in MySQL. (SQL Server 2005 only).

TEXT Variable-length storage with a maximum size of 2GB data


Data Types in SQL – Unicode Character & String

Data Type Description

NCHAR Fixed length with a maximum length of 4,000 characters

NVARCHAR Variable-length storage with a maximum length of 4,000


characters

NVARCHAR(max) Variable-length storage with provided max characters

NTEXT Variable-length storage with a maximum size of 1GB data


Data Types in SQL – Binary Data Types

Data Type Description

BINARY Fixed length with a maximum length of 8,000 bytes

VARBINARY Variable-length storage with a maximum length of 8,000 bytes

VARBINARY(max) Variable-length storage with provided max bytes

IMAGE Variable-length storage with a maximum size of 2 GB binary


data
What is an Operator in SQL?

An operator is a reserved word or a character used primarily in


an SQL statement's WHERE clause to perform operation(s), such
as comparisons and arithmetic operations. These Operators are
used to specify conditions in an SQL statement and to serve as
conjunctions for multiple conditions in a statement.
• Arithmetic operators
• Comparison operators
• Logical operators
• Operators used to negate conditions
SQL Arithmetic Operators
Operator Description Example
Adds values on either side of the operator.
+ (Addition) a + b will give 30

Subtracts right hand operand from left hand


- (Subtraction) operand. a - b will give -10

Multiplies values on either side of the operator.


* (Multiplication) a * b will give 200

Divides left hand operand by right hand


/ (Division) operand. b / a will give 2

Divides left hand operand by right hand


% (Modulus) operand and returns remainder. b % a will give 0
SQL Comparison Operators
Operator Description Example

= Checks if the values of two operands are equal or not, if yes then condition becomes true. (a = b) is not true.
Checks if the values of two operands are equal or not, if values are not equal then condition
!= becomes true. (a != b) is true.

Checks if the values of two operands are equal or not, if values are not equal then condition
<> becomes true. (a <> b) is true.

Checks if the value of left operand is greater than the value of right operand, if yes then
> condition becomes true. (a > b) is not true.

Checks if the value of left operand is less than the value of right operand, if yes then condition
< becomes true. (a < b) is true.

Checks if the value of left operand is greater than or equal to the value of right operand, if yes
>= then condition becomes true. (a >= b) is not true.

Checks if the value of left operand is less than or equal to the value of right operand, if yes
<= then condition becomes true. (a <= b) is true.

Checks if the value of left operand is not less than the value of right operand, if yes then
!< condition becomes true. (a !< b) is false.

Checks if the value of left operand is not greater than the value of right operand, if yes then
!> condition becomes true. (a !> b) is true.
SQL Logical Operators
ALL - The ALL operator is used to compare a value to all values in another value set.
AND - The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
ANY - The ANY operator is used to compare a value to any applicable value in the list
as per the condition.
BETWEEN - The BETWEEN operator is used to search for values that are within a set
of values, given the minimum value and the maximum value.
EXISTS - The EXISTS operator is used to search for the presence of a row in a
specified table that meets a certain criterion.
IN - The IN operator is used to compare a value to a list of literal values that have
been specified.
LIKE - The LIKE operator is used to compare a value to similar values using wildcard
operators.
SQL Logical Operators
NOT
The NOT operator reverses the meaning of the logical operator with which it is used.
Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.

OR
The OR operator is used to combine multiple conditions in an SQL statement's
WHERE clause.

IS NULL
The NULL operator is used to compare a value with a NULL value.

UNIQUE
The UNIQUE operator searches every row of a specified table for uniqueness (no
duplicates).
Some examples

ID | NAME | AGE | ADDRESS | SALARY |


+----+-------------+-------+-----------------+---------------
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Some examples
SELECT AGE FROM CUSTOMERS
SELECT * FROM CUSTOMERS
WHERE EXISTS (SELECT AGE FROM
CUSTOMERS WHERE SALARY > 6500); WHERE AGE > ALL (SELECT AGE
+-----+ FROM CUSTOMERS WHERE
| AGE SALARY > 6500);
|
+-----+
| 32 |
| 25 |
| 23 |
| 25 |
| 27 |
| 22 |
| 24 |
+-----+
Some examples
SELECT * FROM CUSTOMERS
WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);

You might also like