SlideShare a Scribd company logo
1 of 27
INTRODUCTION TO MYSQL AND SIMPLE QUERIES IN SQL
PREPARED BY NEERAJ KUMAR
YEAR 2
SEMESTER 3
BRANCH IT
SUBMITTED TO MR.VIKAS ROHIT(IT DEPT.)
MySQL
AnIntroduction
Whatis MySQL ?
 MySQL is an open source
relational database management
system.
 It includes the SQL server and
client programs for accessing the
server.
 Widely used by web application
developers, together with PHP
and APACHE
 It is pronounced My-es-que-el
(Not My-Sequel)
Where can I get MySQL ?
Some concepts:-
• MYSQL AND SQL
• CLASSIFICATION OF
SQL STATEMENTS
 DDL
COMMANDS
 DML COMMANDS
 TCL COMMANDS
ETC…
SAMPLE DATABASE:-
BEFORE WORKING WITH COMMANDS WE HAVE TO
LOAD A SAMPLE DATABASE.
FOR EXAMPLE WE HAVE DOWNLOADED MENAGERIE
DATABASE FROM THE
MYSQL SITE (dev/mysql/com/doc)AND LOADED IN OUR
MYSQL SEVER.
WE CAN ALSO CREATE OUR OWN DATABASE.
BY USING COMMAND:-
mysql> CREATE DATABASE [IF NOT EXISTS
<DATABSE_NAME>;
MAKING SIMPLE QUERIES:-
1.ACCESSISNG DATABASE:-
USE <DATABASE_NAME>;
EXAMPLE:-
mysql> USE MENAGERIE;
COMMAND FOR VIEWING TABLES:-
mysql> show tables;
THE SELECT COMMAND:-
SELECT COMMAND LETS YOU MAKE QUERIES ON THE DATABASE.IT CAN BE
USED TO RETRIEVE A SUBSET OF ROWS AND COLUMNS FROM ONE OR
MORE TABLES.
SELECT <column name> [, <column name>,…..
From <table name>;
EXAMPLE:-
mysql> SELECT name,owner
->FROM pet;
For selecting all columns:-
SELECT * FROM pet;
name owner
Fluffy Harold
Claws Gwen
Buffy Harold
Fang Benny
Bowser Diane
Chirpy Gwen
Whistler Gwen
OUTPUT:-
CREATING TABLES:-
CREATE TABLE<table_name>
(<column name> <datatype> [(size)],
……………….];
EXAMPLE:-
CREATE TABLE employee
(ecode integer,
Name char(20),
Gender char(1),
Grade char(2),
Gross decimal );
Ecode Ename Gender Gender Gross
1101 Brian M E3 12000
1102 Maithilli F M1 18000
1103 Kushagra F M3 24000
1104 Vansh M M3 24000
1105 Samaira F E2 11000
OUTPUT:-
ELEMINATING REDUNDANT DATA(WITH KEYWORD DISTINCT):-
EXAMPLE:-
SELECT DISTINCT city
FROM suppliers;
IN THE OUTPUT THERE WILL BE NO DUPLICATE ROWS.
SELECTING ALL THE ROWS-ALL KEYWORD:-
EXAMPLE:-
SELECT ALL city FROM suppliers;
IT WILL GIVE THE VALUES OF CITY COLUMN FROM
EVERY ROW OF THE TABLE WITHOUT CONSIDERING THE
DUPLICATE ENTERIES.
city
Delhi
Mumbai
Jaipur
OUTPUT BY USING DISTINCT KEYWORD:-
City
Delhi
Mumbai
Delhi
Banglore
OUTPUT USING ALL KEYWORD
VIEWING STRUCTURE OF A TABLE:-
DESCRIBE|DESC <TABLE_NAME>;
EXAMPLE:-
DESCRIBE|DESC pet;
Field Type Null Key Default
Name Varchar(20) Yes Null
Owner Varchar(20) Yes Null
Species Varchar(20) Yes Null
Sex Char(1) Yes Null
Birth Date Yes Null
Death Date Yes Null
SELECTING SPECIFIC ROWS- WHERE CLAUSE
SELECT <column_name>[, <column_name>,….]
FROM <table_name>
WHERE <condition>;
EXAMPLE:-
mysql> SELECT name,aggregate
->FROM student
->WHERE aggregate >350;
name aggregate
Abu bakar 456
Gurvinder 480
Zubin 412
Simran 378
Fatimah 400
RELATIONAL OPERATPOR:-
EXAMPLE:-
1.TO LIST ALL MEMBERS NOT FROM DELHI
SELECT * FROM suppliers
WHERE city <> ‘delhi’;
LOGICAL OPERATORS:-
EXAMPLE:-
mysql> SELECT * from PET
-> where (species=‘cat’||species=‘dog’)&&sex=‘m’;
OUTPUT:-
name owner species sex birth death
Claws Gwen Cat m 1994-03-17 Null
Fang Benny dog m 1990-08-27 Null
Bowser Diane dog m 1979-08-31 1995-07-29
CONDITION BASED ON RANGE:-
SELECT name, aggregate
FROM students
WHERE aggregate BETWEEN 380 AND 425;
name aggregate
Divyansh Valecha 456
Apoorv Kumar DAS 340
Mehtap Singh Dawar 480
Monika Bansal 150
CONDITION BASED ON A LIST:-
OUTPUT
EXAMPLE:-
mysql> SELECT * FROM pet
-> WHERE species IN (‘bird’, ‘snake’, ‘hamster’);
name owner species sex birth death
Chirpy Gwen bird f 1998-
09-11
Null
Whistler Gwen bird Null 1997-
12-09
Null
Slim Benny snake m 1996-
04-29
Null
Puffball Diane hamster f 1999-
03-30
null
OUTPUT
CONDITION BASED ON PATTERN MATCHES
SQL ALSO INCLUDES A STRING MATCHING OPERATOR:-
1.PERCENT(%) – IT MATCHES ANY SUBSTRING.
2.UNDERSCORE(_)- IT MATCHES ANY CHARACTER.
EXAMPLE:-
1.‘SAN%’ MATCHES ANY STRING BEGINNING WITH ‘SAN’.
2.‘%IDGE%’ MATCHES ANY STRING CONTAINING ‘IDGE’ AS
A SUBSTRING,FOR EXAMPLE ‘RIDGE’, ‘BRIDGES’,
‘CARTRIDGE’ ETC..
3. ‘_ _ _ _’ MATCHES ANY STRING OF ANY 4 CHARACTERS.
THE ‘ LIKE ‘ KEYWORD IS USED TO SELECT ROWS
CONTAINING COLUMNS THAT MATCH A WILDCARD PATTERN.
EXAMPLES:-
1.TO LIST NAMES OF PETS WHO HAVE NAMES ENDING WITH ‘Y’ :
SELECT name FROM pet
WHERE name LIKE ‘%y’ ;
name
Fluffy
Buffy
Chirpy
OUTPUT
Q. WRITE QUERY TO DISPLAY THE NAMES OF PETS HAVING EXACTLY FOUR
LETTERS NAMES ?
mysql> SELECT*FROM pet
->WHERE name LIKE “_ _ _ _ “;
name owner species
Fang Benny dog
Slim Benny snake
OUTPUT
SOME MY SQL FUNCTIONS:-
1. CHAR() – THIS FUNCTION RETURNS THE CHARACTER OF EACH INTEGER
PASSED.
SYNTAX
CHAR(VALUE1 [,VALUE2….])
mysql> CHAR (70,65,67,69) ;
2.CONCAT() – THIS FUNCTION CONCATENATES TWO STRINGS.
SYNTAX:-
CONCAT (STR1,STR2,…)
CHAR (70,65,67,69)
FACE
3.LOWER/LCASE() – THIS FUNCTION CONVERTS A STRING INTO LOWERCASE.
SYNTAX
LOWER/LCASE(STR)
4.UCASE ()
5.SUBSTR() – THIS FUNCTION EXTRACTS A SUBSTRING FROM A STRING.
EXAMPLE:-
mysql> SELECT SUBSTR (‘ABCDEFG’ , 3 , 4) “SUBS” ;
6.TRIM() – THIS FUNCTION REMOVES THE LEADING AND
TRAILING SPACES AND CHARACTER FROM A GIVEN STRING.
EXAMPLE:-
mysql> SELECT TRIM(LEADING ‘X’ FROM ‘XXXXBAR ONEXXXX’);
SUBS
CDEF
OUTPUT
TRIM(LEADING ‘X’ FROM ‘XXXXBAR ONEXXXX’);
BAR ONEXXXX
7. INSTR() – THIS FUNCTION FOR A GIVEN SECOND STRING INTO THE
GIVEN FIRST STRING.
EXAMPLE:-
mysql> SELECT INSTR(‘CORPORATE FLOOR’ , ’OR’) AS INSTRING;
INSTRING
2
Mysqlppt

More Related Content

What's hot

PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenicsGiorgio Cefaro
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackVic Metcalfe
 
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceCtvrtkoncz
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible CodeAnis Ahmad
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHPGuilherme Blanco
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Colin O'Dell
 

What's hot (17)

Variables
VariablesVariables
Variables
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenics
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
DEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent InterfaceDEV Čtvrtkon #76 - Fluent Interface
DEV Čtvrtkon #76 - Fluent Interface
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible Code
 
Table through php
Table through phpTable through php
Table through php
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
 
PHP 101
PHP 101 PHP 101
PHP 101
 

Similar to Mysqlppt (20)

Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sah
SahSah
Sah
 
Mysql
MysqlMysql
Mysql
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
 
SQL
SQLSQL
SQL
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
MySQL for beginners
MySQL for beginnersMySQL for beginners
MySQL for beginners
 
75864 sql
75864 sql75864 sql
75864 sql
 
Sql
SqlSql
Sql
 
Sql
SqlSql
Sql
 
PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
 
Higher SQL
Higher SQLHigher SQL
Higher SQL
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 

Recently uploaded

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Recently uploaded (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Mysqlppt

  • 1. INTRODUCTION TO MYSQL AND SIMPLE QUERIES IN SQL PREPARED BY NEERAJ KUMAR YEAR 2 SEMESTER 3 BRANCH IT SUBMITTED TO MR.VIKAS ROHIT(IT DEPT.)
  • 3. Whatis MySQL ?  MySQL is an open source relational database management system.  It includes the SQL server and client programs for accessing the server.  Widely used by web application developers, together with PHP and APACHE  It is pronounced My-es-que-el (Not My-Sequel)
  • 4. Where can I get MySQL ?
  • 5. Some concepts:- • MYSQL AND SQL • CLASSIFICATION OF SQL STATEMENTS  DDL COMMANDS  DML COMMANDS  TCL COMMANDS ETC…
  • 6.
  • 7. SAMPLE DATABASE:- BEFORE WORKING WITH COMMANDS WE HAVE TO LOAD A SAMPLE DATABASE. FOR EXAMPLE WE HAVE DOWNLOADED MENAGERIE DATABASE FROM THE MYSQL SITE (dev/mysql/com/doc)AND LOADED IN OUR MYSQL SEVER. WE CAN ALSO CREATE OUR OWN DATABASE. BY USING COMMAND:- mysql> CREATE DATABASE [IF NOT EXISTS <DATABSE_NAME>;
  • 8. MAKING SIMPLE QUERIES:- 1.ACCESSISNG DATABASE:- USE <DATABASE_NAME>; EXAMPLE:- mysql> USE MENAGERIE; COMMAND FOR VIEWING TABLES:- mysql> show tables;
  • 9. THE SELECT COMMAND:- SELECT COMMAND LETS YOU MAKE QUERIES ON THE DATABASE.IT CAN BE USED TO RETRIEVE A SUBSET OF ROWS AND COLUMNS FROM ONE OR MORE TABLES. SELECT <column name> [, <column name>,….. From <table name>; EXAMPLE:- mysql> SELECT name,owner ->FROM pet; For selecting all columns:- SELECT * FROM pet;
  • 10. name owner Fluffy Harold Claws Gwen Buffy Harold Fang Benny Bowser Diane Chirpy Gwen Whistler Gwen OUTPUT:-
  • 11. CREATING TABLES:- CREATE TABLE<table_name> (<column name> <datatype> [(size)], ……………….]; EXAMPLE:- CREATE TABLE employee (ecode integer, Name char(20), Gender char(1), Grade char(2), Gross decimal );
  • 12. Ecode Ename Gender Gender Gross 1101 Brian M E3 12000 1102 Maithilli F M1 18000 1103 Kushagra F M3 24000 1104 Vansh M M3 24000 1105 Samaira F E2 11000 OUTPUT:-
  • 13. ELEMINATING REDUNDANT DATA(WITH KEYWORD DISTINCT):- EXAMPLE:- SELECT DISTINCT city FROM suppliers; IN THE OUTPUT THERE WILL BE NO DUPLICATE ROWS. SELECTING ALL THE ROWS-ALL KEYWORD:- EXAMPLE:- SELECT ALL city FROM suppliers; IT WILL GIVE THE VALUES OF CITY COLUMN FROM EVERY ROW OF THE TABLE WITHOUT CONSIDERING THE DUPLICATE ENTERIES.
  • 14. city Delhi Mumbai Jaipur OUTPUT BY USING DISTINCT KEYWORD:- City Delhi Mumbai Delhi Banglore OUTPUT USING ALL KEYWORD
  • 15. VIEWING STRUCTURE OF A TABLE:- DESCRIBE|DESC <TABLE_NAME>; EXAMPLE:- DESCRIBE|DESC pet; Field Type Null Key Default Name Varchar(20) Yes Null Owner Varchar(20) Yes Null Species Varchar(20) Yes Null Sex Char(1) Yes Null Birth Date Yes Null Death Date Yes Null
  • 16. SELECTING SPECIFIC ROWS- WHERE CLAUSE SELECT <column_name>[, <column_name>,….] FROM <table_name> WHERE <condition>; EXAMPLE:- mysql> SELECT name,aggregate ->FROM student ->WHERE aggregate >350; name aggregate Abu bakar 456 Gurvinder 480 Zubin 412 Simran 378 Fatimah 400
  • 17. RELATIONAL OPERATPOR:- EXAMPLE:- 1.TO LIST ALL MEMBERS NOT FROM DELHI SELECT * FROM suppliers WHERE city <> ‘delhi’; LOGICAL OPERATORS:- EXAMPLE:- mysql> SELECT * from PET -> where (species=‘cat’||species=‘dog’)&&sex=‘m’;
  • 18. OUTPUT:- name owner species sex birth death Claws Gwen Cat m 1994-03-17 Null Fang Benny dog m 1990-08-27 Null Bowser Diane dog m 1979-08-31 1995-07-29
  • 19. CONDITION BASED ON RANGE:- SELECT name, aggregate FROM students WHERE aggregate BETWEEN 380 AND 425; name aggregate Divyansh Valecha 456 Apoorv Kumar DAS 340 Mehtap Singh Dawar 480 Monika Bansal 150 CONDITION BASED ON A LIST:- OUTPUT
  • 20. EXAMPLE:- mysql> SELECT * FROM pet -> WHERE species IN (‘bird’, ‘snake’, ‘hamster’); name owner species sex birth death Chirpy Gwen bird f 1998- 09-11 Null Whistler Gwen bird Null 1997- 12-09 Null Slim Benny snake m 1996- 04-29 Null Puffball Diane hamster f 1999- 03-30 null OUTPUT
  • 21. CONDITION BASED ON PATTERN MATCHES SQL ALSO INCLUDES A STRING MATCHING OPERATOR:- 1.PERCENT(%) – IT MATCHES ANY SUBSTRING. 2.UNDERSCORE(_)- IT MATCHES ANY CHARACTER. EXAMPLE:- 1.‘SAN%’ MATCHES ANY STRING BEGINNING WITH ‘SAN’. 2.‘%IDGE%’ MATCHES ANY STRING CONTAINING ‘IDGE’ AS A SUBSTRING,FOR EXAMPLE ‘RIDGE’, ‘BRIDGES’, ‘CARTRIDGE’ ETC.. 3. ‘_ _ _ _’ MATCHES ANY STRING OF ANY 4 CHARACTERS. THE ‘ LIKE ‘ KEYWORD IS USED TO SELECT ROWS CONTAINING COLUMNS THAT MATCH A WILDCARD PATTERN.
  • 22. EXAMPLES:- 1.TO LIST NAMES OF PETS WHO HAVE NAMES ENDING WITH ‘Y’ : SELECT name FROM pet WHERE name LIKE ‘%y’ ; name Fluffy Buffy Chirpy OUTPUT
  • 23. Q. WRITE QUERY TO DISPLAY THE NAMES OF PETS HAVING EXACTLY FOUR LETTERS NAMES ? mysql> SELECT*FROM pet ->WHERE name LIKE “_ _ _ _ “; name owner species Fang Benny dog Slim Benny snake OUTPUT
  • 24. SOME MY SQL FUNCTIONS:- 1. CHAR() – THIS FUNCTION RETURNS THE CHARACTER OF EACH INTEGER PASSED. SYNTAX CHAR(VALUE1 [,VALUE2….]) mysql> CHAR (70,65,67,69) ; 2.CONCAT() – THIS FUNCTION CONCATENATES TWO STRINGS. SYNTAX:- CONCAT (STR1,STR2,…) CHAR (70,65,67,69) FACE
  • 25. 3.LOWER/LCASE() – THIS FUNCTION CONVERTS A STRING INTO LOWERCASE. SYNTAX LOWER/LCASE(STR) 4.UCASE () 5.SUBSTR() – THIS FUNCTION EXTRACTS A SUBSTRING FROM A STRING. EXAMPLE:- mysql> SELECT SUBSTR (‘ABCDEFG’ , 3 , 4) “SUBS” ; 6.TRIM() – THIS FUNCTION REMOVES THE LEADING AND TRAILING SPACES AND CHARACTER FROM A GIVEN STRING. EXAMPLE:- mysql> SELECT TRIM(LEADING ‘X’ FROM ‘XXXXBAR ONEXXXX’); SUBS CDEF
  • 26. OUTPUT TRIM(LEADING ‘X’ FROM ‘XXXXBAR ONEXXXX’); BAR ONEXXXX 7. INSTR() – THIS FUNCTION FOR A GIVEN SECOND STRING INTO THE GIVEN FIRST STRING. EXAMPLE:- mysql> SELECT INSTR(‘CORPORATE FLOOR’ , ’OR’) AS INSTRING; INSTRING 2