SlideShare a Scribd company logo
Database Lab 2
Swapnali Pawar
Swapnali Pawar
The SQL CREATE DATABASE Statement
The CREATE DATABASE statement is used to create
a new SQL database.
Syntax
CREATE DATABASE databasename;
Make sure you have admin privilege before creating any
database. Once a database is created, you can check it in the
list of databases with the following SQL command:
SHOW DATABASES;
DATABASE
Swapnali Pawar
The SQL DROP DATABASE
Statement
The DROP DATABASE statement is used to
drop an existing SQL database.
Syntax
DROP DATABASE databasename;
Note: Be careful before dropping a database. Deleting a
database will result in loss of complete information stored in
the database!
Swapnali Pawar
A. Tables
1.Basic Data Types-
char,varchar,varchar2,long,number,fixed
2.Commands to Create Table-
create table
3.Commands to Handle Tables-
Alter
Drop
Insert
Swapnali Pawar
1.Basic Data Types-
• Each column in a database table is required to have a
name and a data type.
• An SQL developer must decide what type of data that
will be stored inside each column when creating a table.
• The data type is a guideline for SQL to understand what
type of data is expected inside of each column, and it
also identifies how SQL will interact with the stored
data.
Note: Data types might have different names in different
database. And even if the name is the same, the size and
other details may be different! Always check the
documentation!
Swapnali Pawar
In MySQL there are three main data types:
1.String 2.numeric 3.date and time.
1.String Datatype
Swapnali Pawar
2. Numeric Datatypes
Swapnali Pawar
3. date and time
Swapnali Pawar
2.Commands to Create Table-
The CREATE TABLE statement is used to create a new table in a
database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example-
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); Swapnali Pawar
3.Commands to Handle Tables
Alter , Drop , Insert
Example
ALTER TABLE Customers
ADD Email varchar(255);
Swapnali Pawar
The SQL DROP TABLE Statement
The DROP TABLE statement is used to drop an
existing table in a database.
Syntax
DROP TABLE table_name;
Note: Be careful before dropping a table. Deleting a table will
result in loss of complete information stored in the table!
Example
DROP TABLE myfriends;
Swapnali Pawar
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
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, ...);
Swapnali Pawar
B. Commands for Record Handling
1. Update,Select,Delete
2. With arithmetic,comparision,logical operators
• And
• OR
• Between
• In
• Like
3. Order by
4. Group by
Swapnali Pawar
1. Update,Select,Delete
UPDATE Syntax
The UPDATE statement is used to modify the existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE Syntax
The DELETE statement is used to delete existing records in a table.
DELETE FROM table_name WHERE condition;
Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement.
The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all
records in the table will be deleted!
SELECT Syntax
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to select data
from. If you want to select all the fields available in the table, use the following
syntax:
SELECT * FROM table_name;
Swapnali Pawar
2. With arithmetic,comparision,logical operators
(And,OR,Between,In,Like)
AND Syntax- The AND operator displays a record if all the conditions
separated by AND are TRUE.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax- The OR operator displays a record if any of the conditions separated
by OR is TRUE.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax- The NOT operator displays a record if the condition(s) is NOT TRUE.
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Swapnali Pawar
BETWEEN
The BETWEEN command is used to select values within a given range. The values can be numbers, text,
or dates.
The BETWEEN command is inclusive: begin and end values are included.
The following SQL statement selects all products with a price BETWEEN 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
Swapnali Pawar
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character
Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a
question mark (?) instead of the underscore (_).
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SQL LIKE Examples
The following SQL statement selects all customers with a CustomerName starting
with "a":
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%'; Swapnali Pawar
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are
at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are
at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends
with "o"
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
Swapnali Pawar
3. The SQL 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;
Example
SELECT * FROM Customers
ORDER BY Country DESC;
Swapnali Pawar
4. The SQL GROUP BY Statement
The GROUP BY statement groups rows that have the same values into
summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or
more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
Swapnali Pawar
C. SQL Functions
1. Date
2. Numeric
3. Character conversion
4. Group Functions
Avg , Max , Min , Sum , Count
5. Set Operations-
Union , Union all, intersect, minus
Swapnali Pawar
1. Date
Swapnali Pawar
Date Operations :
SELECT CURTIME();
SELECT CURDATE()
SELECT DATE_ADD('1994-16-12',INTERVAL 31 DAY);
SELECT DAYNAME('1994-12-16');
SELECT DAYOFMONTH('1994-12-16');
SELECT DAYOFWEEK('1995-12-16');
SELECT DAYOFYEAR('1995-12-16');
SELECT FROM_DAYS(729669);
Swapnali Pawar
2. Numeric
Swapnali Pawar
ABS(X)
The ABS() function returns the absolute value of X. Consider the following
example −
SQL> SELECT ABS(2);
2. Numeric Functions
CEILING(X)
These functions return the smallest integer value that is not smaller than X. Consider the
following example −
SQL> SELECT CEILING(3.46);
CEILING(3.46)
4
COS(X)
This function returns the cosine of X. The value of X is given in radians.
SQL>SELECT COS(90);
COS(90)
-0.44807361612917
FLOOR(X)
This function returns the largest integer value that is not greater than X.
SQL>SELECT FLOOR(7.55);
FLOOR(7.55)
7 Swapnali Pawar
3. Character conversion
Swapnali Pawar
• SELECT CONCAT(first_name, ' ',last_name) as full_name from myfriends;
• SELECT length(first_name) as full_name from myfriends;
• SELECT length(concat(first_name,last_name)) as full_name from myfriends;
• SELECT lower(first_name) as full_name from myfriends;
• SELECT ucase(first_name) as full_name from myfriends;
• SELECT upper(first_name) as full_name from myfriends;
• SELECT ltrim(first_name) as full_name from myfriends;
• SELECT mid(first_name,1,2) as full_name from myfriends;
• SELECT reverse(first_name) as full_name from myfriends;
String Operation
Swapnali Pawar
4. Group Functions
Avg , Max , Min , Sum , Count
COUNT() Syntax-
The COUNT() function returns the number of rows that matches a
specified criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax-
The AVG() function returns the average value of a numeric
column
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Swapnali Pawar
The SQL MIN() and MAX() Function
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
The MAX() function returns the largest value of the selected
column
The MIN() function returns the smallest value of the selected
column.
Swapnali Pawar
5. Set Operations-
Union , Union all, intersect, minus
UNION
The UNION command combines the result set of two or more
SELECT statements (only distinct values). it will eliminate duplicate
rows from its resultset.
The following SQL statement returns the cities (only distinct values)
from both the "Customers" and the "Suppliers" table:
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Swapnali Pawar
UNION Example
Swapnali Pawar
UNION ALL
The UNION ALL command combines the result set of two
or more SELECT statements (allows duplicate values).
The following SQL statement returns the cities
(duplicate values also) from both the "Customers" and
the "Suppliers" table:
Example
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
Swapnali Pawar
UNION ALL Example
Swapnali Pawar
Intersect
Intersect operation is used to combine two SELECT statements,
but it only retuns the records which are common from
both SELECT statements. In case of Intersect the number of
columns and datatype must be same.
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
Swapnali Pawar
Intersect Example
Swapnali Pawar
MINUS
The Minus operation combines results of two SELECT statements
and return only those in the final result, which belongs to the first
set of the result
SELECT * FROM First
MINUS
SELECT * FROM Second;
Swapnali Pawar
MINUS Operation Example
Swapnali Pawar
Student Activity
• Create table myfriends & execute
all queries on that table
CREATE TABLE myfriends
(
last_name VARCHAR(15) NOT NULL,
first_name VARCHAR(15) NOT NULL,
suffix VARCHAR(5) NULL,
sex VARCHAR(1) NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(2) NOT NULL,
age int
);
Swapnali Pawar
Swapnali Pawar

More Related Content

PDF
SQL JOINS
PDF
View & index in SQL
PPT
Advanced Sql Training
PPTX
DDL,DML,SQL Functions and Joins
PPTX
PPTX
Lab1 select statement
PPTX
1. dml select statement reterive data
SQL JOINS
View & index in SQL
Advanced Sql Training
DDL,DML,SQL Functions and Joins
Lab1 select statement
1. dml select statement reterive data

What's hot (18)

PPTX
Advanced SQL Webinar
PDF
Mysql cheatsheet
PDF
Nested Queries Lecture
DOCX
PPTX
SQL Tutorial for Beginners
PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
DOCX
SQL report
PDF
SQL Overview
PPTX
SQL Server Learning Drive
PPT
MY SQL
PPT
Oracle Sql & PLSQL Complete guide
PPT
SQL Tutorial - Basic Commands
PPTX
SQL Fundamentals
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPT
SQL select statement and functions
PPTX
Null values, insert, delete and update in database
DOC
A must Sql notes for beginners
ODP
SQL Tunning
Advanced SQL Webinar
Mysql cheatsheet
Nested Queries Lecture
SQL Tutorial for Beginners
Database Systems - SQL - DDL Statements (Chapter 3/2)
SQL report
SQL Overview
SQL Server Learning Drive
MY SQL
Oracle Sql & PLSQL Complete guide
SQL Tutorial - Basic Commands
SQL Fundamentals
Database Systems - SQL - DDL Statements (Chapter 3/3)
SQL select statement and functions
Null values, insert, delete and update in database
A must Sql notes for beginners
SQL Tunning
Ad

Similar to Database Management System 1 (20)

PPTX
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
PPTX
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
PPTX
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
PPTX
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
PPTX
PPTX
Oraclesql
PPTX
SQL Data Manipulation language and DQL commands
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
PDF
full detailled SQL notesquestion bank (1).pdf
PPT
Mysql 120831075600-phpapp01
PPTX
SQl data base management and design
PPTX
My SQL.pptx
PPTX
Creating database using sql commands
PPTX
SQL Query
PPTX
PPT
Select To Order By
PDF
Database development coding standards
PPTX
Oracle basic queries
PPTX
SQL PPT.pptx
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
Oraclesql
SQL Data Manipulation language and DQL commands
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
full detailled SQL notesquestion bank (1).pdf
Mysql 120831075600-phpapp01
SQl data base management and design
My SQL.pptx
Creating database using sql commands
SQL Query
Select To Order By
Database development coding standards
Oracle basic queries
SQL PPT.pptx
Ad

More from Swapnali Pawar (18)

PDF
Unit 3 introduction to android
PDF
Unit 1-Introduction to Mobile Computing
PDF
Unit 2.design mobile computing architecture
PDF
Introduction to ios
PDF
Fresher interview tips demo
PDF
Introduction to android
PDF
Android Introduction
PDF
Unit 2.design computing architecture 2.1
PDF
Unit 2 Design mobile computing architecture MC1514
PDF
Design computing architecture ~ Mobile Technologies
PDF
Exception Handling
PDF
Mobile technology-Unit 1
PDF
Mobile Technology 3
PDF
Web Programming& Scripting Lab
PDF
Mobile Technology
PDF
Mobile Technology
PDF
web programming & scripting 2
PDF
web programming & scripting
Unit 3 introduction to android
Unit 1-Introduction to Mobile Computing
Unit 2.design mobile computing architecture
Introduction to ios
Fresher interview tips demo
Introduction to android
Android Introduction
Unit 2.design computing architecture 2.1
Unit 2 Design mobile computing architecture MC1514
Design computing architecture ~ Mobile Technologies
Exception Handling
Mobile technology-Unit 1
Mobile Technology 3
Web Programming& Scripting Lab
Mobile Technology
Mobile Technology
web programming & scripting 2
web programming & scripting

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Pre independence Education in Inndia.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
01-Introduction-to-Information-Management.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
master seminar digital applications in india
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Sports Quiz easy sports quiz sports quiz
Pre independence Education in Inndia.pdf
GDM (1) (1).pptx small presentation for students
01-Introduction-to-Information-Management.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Renaissance Architecture: A Journey from Faith to Humanism
master seminar digital applications in india
Insiders guide to clinical Medicine.pdf
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
VCE English Exam - Section C Student Revision Booklet

Database Management System 1

  • 1. Database Lab 2 Swapnali Pawar Swapnali Pawar
  • 2. The SQL CREATE DATABASE Statement The CREATE DATABASE statement is used to create a new SQL database. Syntax CREATE DATABASE databasename; Make sure you have admin privilege before creating any database. Once a database is created, you can check it in the list of databases with the following SQL command: SHOW DATABASES; DATABASE Swapnali Pawar
  • 3. The SQL DROP DATABASE Statement The DROP DATABASE statement is used to drop an existing SQL database. Syntax DROP DATABASE databasename; Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database! Swapnali Pawar
  • 4. A. Tables 1.Basic Data Types- char,varchar,varchar2,long,number,fixed 2.Commands to Create Table- create table 3.Commands to Handle Tables- Alter Drop Insert Swapnali Pawar
  • 5. 1.Basic Data Types- • Each column in a database table is required to have a name and a data type. • An SQL developer must decide what type of data that will be stored inside each column when creating a table. • The data type is a guideline for SQL to understand what type of data is expected inside of each column, and it also identifies how SQL will interact with the stored data. Note: Data types might have different names in different database. And even if the name is the same, the size and other details may be different! Always check the documentation! Swapnali Pawar
  • 6. In MySQL there are three main data types: 1.String 2.numeric 3.date and time. 1.String Datatype Swapnali Pawar
  • 8. 3. date and time Swapnali Pawar
  • 9. 2.Commands to Create Table- The CREATE TABLE statement is used to create a new table in a database. Syntax CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... ); Example- CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ); Swapnali Pawar
  • 10. 3.Commands to Handle Tables Alter , Drop , Insert Example ALTER TABLE Customers ADD Email varchar(255); Swapnali Pawar
  • 11. The SQL DROP TABLE Statement The DROP TABLE statement is used to drop an existing table in a database. Syntax DROP TABLE table_name; Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in the table! Example DROP TABLE myfriends; Swapnali Pawar
  • 12. The SQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. 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, ...); Swapnali Pawar
  • 13. B. Commands for Record Handling 1. Update,Select,Delete 2. With arithmetic,comparision,logical operators • And • OR • Between • In • Like 3. Order by 4. Group by Swapnali Pawar
  • 14. 1. Update,Select,Delete UPDATE Syntax The UPDATE statement is used to modify the existing records in a table. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; DELETE Syntax The DELETE statement is used to delete existing records in a table. DELETE FROM table_name WHERE condition; Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted! SELECT Syntax The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set. SELECT column1, column2, ... FROM table_name; Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax: SELECT * FROM table_name; Swapnali Pawar
  • 15. 2. With arithmetic,comparision,logical operators (And,OR,Between,In,Like) AND Syntax- The AND operator displays a record if all the conditions separated by AND are TRUE. SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...; OR Syntax- The OR operator displays a record if any of the conditions separated by OR is TRUE. SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; NOT Syntax- The NOT operator displays a record if the condition(s) is NOT TRUE. SELECT column1, column2, ... FROM table_name WHERE NOT condition; Swapnali Pawar
  • 16. BETWEEN The BETWEEN command is used to select values within a given range. The values can be numbers, text, or dates. The BETWEEN command is inclusive: begin and end values are included. The following SQL statement selects all products with a price BETWEEN 10 and 20: Example SELECT * FROM Products WHERE Price BETWEEN 10 AND 20; SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20; IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...); Example SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK'); Swapnali Pawar
  • 17. The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: • The percent sign (%) represents zero, one, or multiple characters • The underscore sign (_) represents one, single character Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of the underscore (_). LIKE Syntax SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern; SQL LIKE Examples The following SQL statement selects all customers with a CustomerName starting with "a": Example SELECT * FROM Customers WHERE CustomerName LIKE 'a%'; Swapnali Pawar
  • 18. LIKE Operator Description WHERE CustomerName LIKE 'a%' Finds any values that start with "a" WHERE CustomerName LIKE '%a' Finds any values that end with "a" WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o" Here are some examples showing different LIKE operators with '%' and '_' wildcards: Swapnali Pawar
  • 19. 3. The SQL 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; Example SELECT * FROM Customers ORDER BY Country DESC; Swapnali Pawar
  • 20. 4. The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns. GROUP BY Syntax SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s); Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country Swapnali Pawar
  • 21. C. SQL Functions 1. Date 2. Numeric 3. Character conversion 4. Group Functions Avg , Max , Min , Sum , Count 5. Set Operations- Union , Union all, intersect, minus Swapnali Pawar
  • 23. Date Operations : SELECT CURTIME(); SELECT CURDATE() SELECT DATE_ADD('1994-16-12',INTERVAL 31 DAY); SELECT DAYNAME('1994-12-16'); SELECT DAYOFMONTH('1994-12-16'); SELECT DAYOFWEEK('1995-12-16'); SELECT DAYOFYEAR('1995-12-16'); SELECT FROM_DAYS(729669); Swapnali Pawar
  • 25. ABS(X) The ABS() function returns the absolute value of X. Consider the following example − SQL> SELECT ABS(2); 2. Numeric Functions CEILING(X) These functions return the smallest integer value that is not smaller than X. Consider the following example − SQL> SELECT CEILING(3.46); CEILING(3.46) 4 COS(X) This function returns the cosine of X. The value of X is given in radians. SQL>SELECT COS(90); COS(90) -0.44807361612917 FLOOR(X) This function returns the largest integer value that is not greater than X. SQL>SELECT FLOOR(7.55); FLOOR(7.55) 7 Swapnali Pawar
  • 27. • SELECT CONCAT(first_name, ' ',last_name) as full_name from myfriends; • SELECT length(first_name) as full_name from myfriends; • SELECT length(concat(first_name,last_name)) as full_name from myfriends; • SELECT lower(first_name) as full_name from myfriends; • SELECT ucase(first_name) as full_name from myfriends; • SELECT upper(first_name) as full_name from myfriends; • SELECT ltrim(first_name) as full_name from myfriends; • SELECT mid(first_name,1,2) as full_name from myfriends; • SELECT reverse(first_name) as full_name from myfriends; String Operation Swapnali Pawar
  • 28. 4. Group Functions Avg , Max , Min , Sum , Count COUNT() Syntax- The COUNT() function returns the number of rows that matches a specified criterion. SELECT COUNT(column_name) FROM table_name WHERE condition; AVG() Syntax- The AVG() function returns the average value of a numeric column SELECT AVG(column_name) FROM table_name WHERE condition; SUM() Syntax The SUM() function returns the total sum of a numeric column. SELECT SUM(column_name) FROM table_name WHERE condition; Swapnali Pawar
  • 29. The SQL MIN() and MAX() Function MIN() Syntax SELECT MIN(column_name) FROM table_name WHERE condition; MAX() Syntax SELECT MAX(column_name) FROM table_name WHERE condition; The MAX() function returns the largest value of the selected column The MIN() function returns the smallest value of the selected column. Swapnali Pawar
  • 30. 5. Set Operations- Union , Union all, intersect, minus UNION The UNION command combines the result set of two or more SELECT statements (only distinct values). it will eliminate duplicate rows from its resultset. The following SQL statement returns the cities (only distinct values) from both the "Customers" and the "Suppliers" table: Example SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City; Swapnali Pawar
  • 32. UNION ALL The UNION ALL command combines the result set of two or more SELECT statements (allows duplicate values). The following SQL statement returns the cities (duplicate values also) from both the "Customers" and the "Suppliers" table: Example SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City; Swapnali Pawar
  • 34. Intersect Intersect operation is used to combine two SELECT statements, but it only retuns the records which are common from both SELECT statements. In case of Intersect the number of columns and datatype must be same. SELECT * FROM First INTERSECT SELECT * FROM Second; Swapnali Pawar
  • 36. MINUS The Minus operation combines results of two SELECT statements and return only those in the final result, which belongs to the first set of the result SELECT * FROM First MINUS SELECT * FROM Second; Swapnali Pawar
  • 38. Student Activity • Create table myfriends & execute all queries on that table CREATE TABLE myfriends ( last_name VARCHAR(15) NOT NULL, first_name VARCHAR(15) NOT NULL, suffix VARCHAR(5) NULL, sex VARCHAR(1) NULL, city VARCHAR(20) NOT NULL, state VARCHAR(2) NOT NULL, age int ); Swapnali Pawar