You are on page 1of 47

Introduction to 

SQL

• What is SQL?
• SQL stands for Structured Query Language
What Can SQL do?

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
Database Tables

• A database most often contains one or more


tables.
• Each table is identified by a name (e.g.
"Customers" or "Orders").
• Tables contain records (rows) with data.
Customer table
SQL lab
•Open XAMP
• Shell
•Open mysql
• type mysql –u root –p with pswd
• mysql –u root without pswd

• The SQL CREATE DATABASE


• syntax
• CREATE DATABASE databasename;
•Example
•Create database
• create database testDB;
•show databases
• show databases;

SQL Lab
• Drop database
• Syntax
• DROP DATABASE databasename;
• Example
• DROP DATABASE testDB;

• Use database
• use testDB;
CREATE TABLE
• Syntax
• CREATE TABLE table_name(
• column1 datatype,
• column2 datatype,
• column3 datatype,
• ..... columnN datatype,
• PRIMARY KEY( one or more columns ) );
Example table
• CREATE TABLE CUSTOMERS(
• ID INT NOT NULL,
• NAME VARCHAR (20) NOT NULL,
• AGE INT NOT NULL,
• ADDRESS CHAR (25),
• SALARY DECIMAL (18, 2),
• PRIMARY KEY (ID));
Describe Table
• You can verify if your table has been created
successfully by looking at the message
displayed by the SQL server, otherwise you
can use the DESC command as follows −
• SQL> DESC CUSTOMERS;
DROP TABLE
• Syntax
• The basic syntax of this DROP TABLE statement
is as follows −
• DROP TABLE table_name;
Show tables
• Syntax
• show tables;
• Example
• show tables;
Insert data
• INSERT INTO Statement is used to add new rows of
data to a table in the database.
• Syntax
• INSERT INTO TABLE_NAME (
• column1,
• column2,
• column3,
• ...columnN)
• VALUES (value1, value2, value3,...valueN);
Example
• INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
• INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(2, 'Khilan', 25, 'Delhi', 1500.00 );
• INSERT INTO CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY) VALUES
(3, 'kaushik', 23, 'Kota', 2000.00 );
• INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(4, 'Chaitali', 25, 'Mumbai', 6500.00 );
• INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(5, 'Hardik', 27, 'Bhopal', 8500.00 );
• INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(6, 'Komal', 22, 'MP', 4500.00 );
Create a table Employee
Create a table Student
Select statement
• SELECT statement is used to fetch the data from a
database table which returns this data in the form
of a result table.
• Syntax
• SELECT column1, column2, columnN FROM
table_name;
• If you want to fetch all the fields available in the
field, then you can use the following syntax.
• SELECT * FROM table_name;
Select statement
• The following code is an example, which
would fetch the ID, Name and Salary fields of
the customers available in CUSTOMERS table.
• SELECT ID, NAME, SALARY FROM
CUSTOMERS;
This would produce the following result 
• The SQL WHERE clause is used to specify a condition
while fetching the data from a single table or by
joining with multiple tables. If the given condition is
satisfied, then only it returns a specific value from
the table.
• You should use the WHERE clause to filter the
records and fetching only the necessary records.
• The WHERE clause is not only used in the SELECT
statement, but it is also used in the UPDATE, DELETE
statement, etc.,
• Syntax
• SELECT column1, column2, columnN FROM
table_name WHERE [condition]
• example
• The following code is an example which would
fetch the ID, Name and Salary fields from the
CUSTOMERS table, where the salary is greater
than 2000
Where…
• SELECT ID, NAME, SALARY FROM CUSTOMERS
WHERE SALARY > 2000;
• This would produce the following result -
DAY 2
SQL - AND and OR Conjunctive Operators

• The SQL AND & OR operators are used to combine multiple


conditions to narrow data in an SQL statement. These two
operators are called as the conjunctive operators.
• The AND Operator
• Syntax
• SELECT column1, column2, columnN FROM table_name
WHERE [condition1] AND [condition2]...AND
[conditionN];
• SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE
SALARY > 2000 AND age < 25;
OR
The OR Operator
• Syntax
• SELECT column1, column2, columnN FROM
table_name WHERE [condition1] OR
[condition2]...OR [conditionN]
• SELECT ID, NAME, SALARY FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;
UPDATE Query
• The SQL UPDATE Query is used to modify the existing
records in a table. You can use the WHERE clause
with the UPDATE query to update the selected rows,
otherwise all the rows would be affected.
• Syntax
• UPDATE table_name SET column1 = value1, column2
= value2...., columnN = valueN WHERE [condition];
• UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE
ID = 6;
…..
…Update
• UPDATE CUSTOMERS SET ADDRESS = 'Pune',
SALARY = 1000.00;
DELETE Query
• The SQL DELETE Query is used to delete the
existing records from a table.
• Syntax
• DELETE FROM table_name WHERE
[condition];
• DELETE FROM CUSTOMERS WHERE ID = 6;
Now, the CUSTOMERS table would have the
following records.

• If you want to DELETE all the records from the CUSTOMERS table,
you do not need to use the WHERE clause and the DELETE query
would be as follows −
• SQL> DELETE FROM CUSTOMERS;
LIKE Clause
• The SQL LIKE clause is used to compare a value to similar
values using wildcard operators. There are two wildcards used
in conjunction with the LIKE operator.
• The percent sign (%)
• The underscore (_)
• Syntax
• SELECT FROM table_name WHERE column LIKE 'XXXX%' or
• SELECT FROM table_name WHERE column LIKE '%XXXX%' or
SELECT FROM table_name WHERE column LIKE 'XXXX_' or
SELECT FROM table_name WHERE column LIKE '_XXXX' or
SELECT FROM table_name WHERE column LIKE '_XXXX_'
….
• SELECT * FROM CUSTOMERS WHERE SALARY
LIKE '200%';
TOP, LIMIT or ROWNUM Clause
• The SQL TOP clause is used to fetch a TOP N
number or X percent records from a table.
• Syntax
• SELECT TOP number|percent column_name(s)
FROM table_name WHERE [condition]
• SELECT TOP 3 * FROM CUSTOMERS;
• SELECT * FROM CUSTOMERS LIMIT 3;
• SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;
ORDER BY clause
• The SQL ORDER BY clause is used to sort the data
in ascending or descending order, based on one
or more columns.
• Syntax
• SELECT column-list FROM table_name [WHERE
condition] [ORDER BY column1, column2, ..
columnN] [ASC | DESC];
• SELECT * FROM CUSTOMERS ORDER BY NAME,
SALARY;
ORDER…
• SELECT * FROM CUSTOMERS ORDER BY NAME
DESC;
GROUP BY clause
• The SQL GROUP BY clause is used in collaboration with
the SELECT statement to arrange identical data into
groups. This GROUP BY clause follows the WHERE clause
in a SELECT statement and precedes the ORDER BY clause
• Syntax
• SELECT column1, column2 FROM table_name WHERE
[ conditions ] GROUP BY column1, column2 ORDER BY
column1, column2
• SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP
BY NAME;
Now, let us look at a table where the CUSTOMERS table
has the following records with duplicate names −

• if you want to know the total amount of salary on each


customer, then the GROUP BY query would be as follows

….

• SELECT NAME, SUM(SALARY) FROM


CUSTOMERS GROUP BY NAME;
SQL - Distinct Keyword
• The SQL DISTINCT keyword is used in
conjunction with the SELECT statement to
eliminate all the duplicate records and
fetching only unique records.
• Syntax
• SELECT DISTINCT column1,
column2,.....columnN FROM table_name
WHERE [condition]
….
• SELECT SALARY FROM CUSTOMERS ORDER BY
SALARY;
• let us see how the following SELECT query
returns the duplicate salary records.
• This would produce the following result,
where the salary (2000) is coming twice which
is a duplicate record from the original table.
• SELECT DISTINCT SALARY FROM CUSTOMERS
ORDER BY SALARY;
• This would produce the following result where
we do not have any duplicate entry.
SORTING Results
• The SQL ORDER BY clause is used to sort the data
in ascending or descending order, based on one
or more columns.
• Syntax
• SELECT column-list FROM table_name [WHERE
condition] [ORDER BY column1, column2, ..
columnN] [ASC | DESC];
• SELECT * FROM CUSTOMERS ORDER BY NAME,
SALARY;
……….
• SELECT * FROM CUSTOMERS ORDER BY NAME
DESC;

You might also like