0% found this document useful (0 votes)
2 views

Sql

SQL is the standard language for managing relational databases, allowing users to insert, search, update, and delete records. Key commands include SELECT, UPDATE, DELETE, and INSERT INTO, with additional functionalities like filtering with WHERE and sorting with ORDER BY. SQL also supports various operations such as JOINs, aggregate functions, and database/table management commands.

Uploaded by

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

Sql

SQL is the standard language for managing relational databases, allowing users to insert, search, update, and delete records. Key commands include SELECT, UPDATE, DELETE, and INSERT INTO, with additional functionalities like filtering with WHERE and sorting with ORDER BY. SQL also supports various operations such as JOINs, aggregate functions, and database/table management commands.

Uploaded by

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

What is SQL?

SQL is the standard language for dealing with Relational Databases.

SQL is used to insert, search, update, and delete database records.

database systems require a semicolon at the end of each SQL statement.

How to Use SQL


The following SQL statement selects all the records in the "Customers" table:

SELECT * FROM Customers;

Some of The Most Important SQL


Commands
●​ SELECT - extracts data from a database
●​ UPDATE - updates data in a database
●​ DELETE - deletes data from a database
●​ INSERT INTO - inserts new data into a database
●​ CREATE DATABASE - creates a new database
●​ ALTER DATABASE - modifies a database
●​ CREATE TABLE - creates a new table
●​ ALTER TABLE - modifies a table
●​ DROP TABLE - deletes a table
●​ CREATE INDEX - creates an index (search key)
●​ DROP INDEX - deletes an index
The MySQL WHERE Clause
The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

WHERE Syntax

SELECT column1, column2, ...

FROM table_name

WHERE condition;
SELECT * FROM Customers

WHERE Country = 'Mexico';

MySQL AND, OR and NOT Operators


The MySQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one
condition:
●​ The AND operator displays a record if all the conditions separated by AND
are TRUE.
●​ The OR operator displays a record if any of the conditions separated by OR
is TRUE.
●​ The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax

SELECT column1, column2, ...

FROM table_name

WHERE condition1 AND condition2 AND condition3 ...;

OR Syntax

SELECT column1, column2, ...

FROM table_name

WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax

SELECT column1, column2, ...

FROM table_name

WHERE NOT condition;


The MySQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.

The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

SELECT * FROM Customers


ORDER BY Country;

ORDER BY DESC Example


The following SQL statement selects all customers from the "Customers" table,
sorted DESCENDING by the "Country" column.

SELECT * FROM Customers

ORDER BY Country DESC;


SELECT * FROM Customers

ORDER BY Country ASC, CustomerName DESC;

The MySQL INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)

VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:

INSERT INTO table_name

VALUES (value1, value2, value3, ...);


INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger',
'4006', 'Norway');

How to Test for NULL Values?


It is not possible to test for NULL values with comparison operators, such as =,
<, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.

IS NULL Syntax

SELECT column_names

FROM table_name

WHERE column_name IS NULL;

IS NOT NULL Syntax

SELECT column_names
FROM table_name

WHERE column_name IS NOT NULL;

The MySQL UPDATE Statement


The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
The MySQL DELETE Statement
The DELETE statement is used to delete existing records in a table.

DELETE Syntax

DELETE FROM table_name WHERE condition;

The MySQL LIMIT Clause


The LIMIT clause is used to specify the number of records to return.

The LIMIT clause is useful on large tables with thousands of records. Returning
a large number of records can impact performance.

LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

eg:SELECT * FROM Customers LIMIT 3;


MySQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;

SELECT MAX(column_name)
FROM table_name
WHERE condition;

MySQL COUNT(), AVG() and SUM()


Functions
The COUNT() function returns the number of rows that matches a specified
criterion.

COUNT() Syntax

SELECT COUNT(column_name)

FROM table_name
WHERE condition;

The AVG() function returns the average value of a numeric column.

AVG() Syntax

SELECT AVG(column_name)

FROM table_name

WHERE condition;

The SUM() function returns the total sum of a numeric column.

SUM() Syntax

SELECT SUM(column_name)

FROM table_name

WHERE condition

A JOIN clause is used to combine rows from two or more tables, based on a related
column between them

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate

FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

Different Types of SQL JOINs


Here are the different types of the JOINs in SQL:

●​ (INNER) JOIN: Returns records that have matching values in both tables
●​ LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
●​ RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
●​ FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table

CREATION OF DATABASE:

1)CREATE DATABASE databasename;


2)CREATION OF TABLE:
CREATE TABLE table_name (
​ column1 datatype,
​ column2 datatype,
​ column3 datatype,
....
);

EG:
3)DROP TABLE:
The DROP TABLE statement is used to drop an existing table in a database.

Syntax

DROP TABLE table_name;

4)TRUNCATE:

The TRUNCATE TABLE statement is used to delete the data inside a table, but
not the table itself.

Syntax:TRUNCATE TABLE table_name;

5) ALTER TABLE:
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.

The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.

SYNTAX TO ADD COLUMN:

ALTER TABLE table_name


ADD column_name datatype;

EXAMPLE:
ALTER TABLE Customers
ADD Email varchar(255);

SYNTAX TO DROP COLUMN:

ALTER TABLE table_name


DROP COLUMN column_name;

EXAMPLE:
ALTER TABLE Customers
DROP COLUMN Email;

You might also like