SlideShare a Scribd company logo
Crash course in SQL
www.skewcode.com
MySQL - Installation
DOWNLOAD
○ https://dev.mysql.com/downloads
■ MySQL Community Server
■ MySQL Shell
■ [OPTIONAL] MySQL Workbench
○ https://dev.mysql.com/doc/index-other.html
■ Example databases -> world database
Data Types
Data types
● Numeric types (INT, DECIMAL, FLOAT, DOUBLE, BIT)
● Date & Time types (DATE, TIME, DATETIME, TIMESTAMP)
● String types (CHAR, VARCHAR, TEXT, ENUM)
Data Definition Statements
Database related operations
List all databases SHOW DATABASES;
Create a new database. CREATE DATABASE mydb1;
Create a new database (if not existing). CREATE DATABASE IF NOT EXISTS mydb1;
Use an existing database. USE mydb1;
List current database. SELECT DATABASE();
Delete existing database. DROP DATABASE mydb1;
Delete existing database (if existing). DROP DATABASE IF EXISTS mydb1;
Data Definition Statements
Table related operations
List all tables in current database SHOW TABLES;
See structure of a specific table. DESCRIBE mytable1;
Create a new table (if not existing). CREATE TABLE IF NOT EXISTS mytable1
(
serialnum INT,
name VARCHAR(20),
location ENUM(‘BLR’, ‘BOM’, ‘DEL’)
);
Delete existing table (if existing). DROP TABLE IF EXISTS mytable1;
Data Definition Statements
Table related operations
Adding columns. ALTER TABLE mytable1
ADD COLUMN firstname varchar(25),
ADD COLUMN lastname varchar(40);
Deleting columns. ALTER TABLE mytable1
DROP COLUMN firstname,
DROP COLUMN lastname;
Modifying columns. ALTER TABLE mytable1
MODIFY COLUMN firstname varchar(25),
MODIFY COLUMN lastname varchar(40);
Data Definition Statements
Constraints
NOT NULL CREATE TABLE mytable1 (col1 INT NOT NULL);
PRIMARY KEY CREATE TABLE mytable1 (col1 INT, PRIMARY KEY (col1));
UNIQUE CREATE TABLE mytable1 (col1 INT UNIQUE);
FOREIGN KEY CREATE TABLE mytable1
(
col1 INT, FOREIGN KEY(col1) REFERENCES mytable2(mycol)
);
DEFAULT CREATE TABLE mytable1 (col1 INT DEFAULT 100);
Data Manipulation Statements
Adding, deleting and updating data (rows)
Add a new row (data in all
columns).
INSERT INTO mytable1
VALUES (‘10’, ‘mithun’, ‘shanbhag’);
Add a new row (data in
specific columns).
INSERT INTO mytable1 (firstname, lastname)
VALUES (‘mithun’, ‘shanbhag’);
Update row(s). UPDATE mytable1
SET firstname = ‘sachin’, lastname = ‘tendulkar’
WHERE serialnum = 101;
Delete row(s). DELETE FROM mytable1
WHERE firstname = ‘mithun’;
Delete all row(s). DELETE * FROM mytable1;
Data Query Statements
● Data Query Statements
○ Filtering Operations (WHERE)
○ Sorting Operations (ORDER BY)
○ Set Operations (DISTINCT, UNION, UNION ALL)
○ Paging Operations (LIMIT)
○ Aliasing Operations (AS)
○ Aggregation Operations (MIN, MAX, AVERAGE, SUM, COUNT)
○ Grouping Operations (GROUP BY)
○ Join Operations (JOIN)
Data Query Statements
Filtering Operations (WHERE)
Extract cities in USA. SELECT *
FROM city
WHERE countrycode = ’usa’;
Extract cities in USA with
population greater than
1,000,000.
SELECT *
FROM city
WHERE countrycode = ’usa’ AND population >
1000000;
Extract cities in USA or
cities with population
greater than 1,000,000.
SELECT *
FROM city
WHERE countrycode = ‘usa’ OR population > 1000000;
Data Query Statements
Filtering Operations (WHERE)
Extract countries starting
with ‘B’.
SELECT *
FROM country
WHERE name LIKE ’b%’;
Extract countries starting
with ‘B’ and ending with ‘a’.
SELECT *
FROM country
WHERE name LIKE ’b%a’;
Extract countries which
have 5 letters in their
name.
SELECT *
FROM country
WHERE name LIKE ’_____’;
Data Query Statements
Filtering Operations (WHERE)
Extract countries with no
head of state.
SELECT *
FROM country
WHERE headofstate IS NULL;
Extract countries which
have heads of state.
SELECT *
FROM country
WHERE headofstate IS NOT NULL;
Extract countries
beginning from letter ‘W’
onwards.
SELECT *
FROM country
WHERE name > ’w’;
Extract countries with
population between
10,000 and 20,000.
SELECT *
FROM country
WHERE population BETWEEN 10000 AND 20000;
Data Query Statements
Sorting Operations (ORDER BY)
Sort countries by name. SELECT *
FROM country
ORDER BY name;
Sort countries by population (descending). SELECT *
FROM country
ORDER BY population DESC;
Sort countries by continent, then by
population (descending).
SELECT *
FROM country
ORDER BY continent, population DESC;
Data Query Statements
Set Operations (DISTINCT)
Extract unique continent names SELECT DISTINCT continent
FROM country;
Extract unique countrycode and district
combinations
SELECT DISTINCT countrycode, district
FROM city;
Data Query Statements
Set Operations (UNION)
● Combines the result-set of two or more SELECT statements.
● Each SELECT statement must have the same number of columns, with ‘similar’ data types and in the
same order.
● UNION supports only ‘distinct’ values, however UNION ALL allows duplicates.
Combine the list of country codes from
the ‘city’ and ‘countrylanguage’ tables.
SELECT countrycode FROM city
UNION
SELECT countrycode FROM countrylanguage
ORDER BY countrycode;
Data Query Statements
Paging Operations (LIMIT)
● Constrain the number of rows returned in result set.
Extract top 5 countries, sorted by
population.
SELECT *
FROM country
ORDER BY population DESC
LIMIT 5;
Extract countries 6 to 10, sorted by
population.
SELECT *
FROM country
ORDER BY population DESC
LIMIT 5, 5;
Data Query Statements
Aliasing Operations (AS)
● Gives a table (or column) a temporary, readable name (alias).
● The alias exists only for the duration of the query.
Aliasing a column name. SELECT name AS cityname, population
FROM country
WHERE cityname = ‘new york’;
Aliasing a table name. SELECT name, population
FROM country as c
WHERE c.name = ‘new york’;
Data Query Statements
Aggregation Operations (MIN, MAX, AVG, SUM, COUNT)
Least population. SELECT MIN(population)
FROM country;
Most population. SELECT MAX(population)
FROM country;
Average population. SELECT AVG(population)
FROM country;
Total population. SELECT SUM(population)
FROM country;
Total number of countries. SELECT COUNT(name)
FROM country;
Data Query Statements
Grouping Operations (GROUP BY)
● Groups the result set using a ‘key’.
● The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG).
Find number of countries in each continent. SELECT continent, count(name)
FROM country
GROUP BY continent;
Group Swedish districts by total
population.
SELECT district, sum(population)
FROM city
WHERE countrycode = ‘swe’
GROUP BY district;
Data Query Statements
Join Operations
INNER JOIN LEFT OUTER
JOIN
RIGHT OUTER
JOIN
FULL OUTER
JOIN
Data Query Statements
Inner Join
Display continent
information for cities.
SELECT id, city.name, countrycode, district, continent
FROM city
INNER JOIN country ON city.CountryCode = country.code;
References
● https://dev.mysql.com/doc/refman/5.7/en/
● https://www.w3schools.com/sql/default.asp

More Related Content

What's hot

05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
rehaniltifat
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
V.V.Vanniaperumal College for Women
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
Ahmed Yaseen
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
Ankit Dubey
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
Java arrays
Java arraysJava arrays
Java arrays
Kuppusamy P
 
User defined functions
User defined functionsUser defined functions
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
VARSHAKUMARI49
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
Bwsrang Basumatary
 
SQL to Hive Cheat Sheet
SQL to Hive Cheat SheetSQL to Hive Cheat Sheet
SQL to Hive Cheat Sheet
Hortonworks
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
Mohan Kumar.R
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
Sachidananda M H
 
SQL Views
SQL ViewsSQL Views
SQL Views
Aaron Buma
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
Nexus
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
DataminingTools Inc
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
sinhacp
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
tlvd
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
DataminingTools Inc
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 

What's hot (20)

05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Java arrays
Java arraysJava arrays
Java arrays
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
SQL to Hive Cheat Sheet
SQL to Hive Cheat SheetSQL to Hive Cheat Sheet
SQL to Hive Cheat Sheet
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
4. plsql
4. plsql4. plsql
4. plsql
 

Similar to Crash course in sql

SQL
SQLSQL
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
ArjayBalberan1
 
Sql
SqlSql
Lecture5-SQL.docx
Lecture5-SQL.docxLecture5-SQL.docx
Lecture5-SQL.docx
ismailaboshatra
 
1670595076250.pdf
1670595076250.pdf1670595076250.pdf
1670595076250.pdf
GaneshR321894
 
Cheat sheet SQL commands with examples and easy understanding
Cheat sheet SQL commands with examples and easy understandingCheat sheet SQL commands with examples and easy understanding
Cheat sheet SQL commands with examples and easy understanding
VivekanandaGN1
 
SQL 🌟🌟🔥.pdf
SQL 🌟🌟🔥.pdfSQL 🌟🌟🔥.pdf
SQL 🌟🌟🔥.pdf
LightWolf2
 
SQL learning notes and all code.pdf
SQL learning notes and all code.pdfSQL learning notes and all code.pdf
SQL learning notes and all code.pdf
79TarannumMulla
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schools
farhan516
 
Dbms
DbmsDbms
BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
BITS
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
Ranjit273515
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...
Moatasim Magdy
 
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
prashant0000
 
CS 542 Overview of query processing
CS 542 Overview of query processingCS 542 Overview of query processing
CS 542 Overview of query processing
J Singh
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
Swapnali Pawar
 
SQL report
SQL reportSQL report
SQL report
Ahmad Zahid
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
TamilHunt
 
Sql
SqlSql

Similar to Crash course in sql (20)

SQL
SQLSQL
SQL
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
 
Sql
SqlSql
Sql
 
Lecture5-SQL.docx
Lecture5-SQL.docxLecture5-SQL.docx
Lecture5-SQL.docx
 
1670595076250.pdf
1670595076250.pdf1670595076250.pdf
1670595076250.pdf
 
Cheat sheet SQL commands with examples and easy understanding
Cheat sheet SQL commands with examples and easy understandingCheat sheet SQL commands with examples and easy understanding
Cheat sheet SQL commands with examples and easy understanding
 
SQL 🌟🌟🔥.pdf
SQL 🌟🌟🔥.pdfSQL 🌟🌟🔥.pdf
SQL 🌟🌟🔥.pdf
 
SQL learning notes and all code.pdf
SQL learning notes and all code.pdfSQL learning notes and all code.pdf
SQL learning notes and all code.pdf
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schools
 
Dbms
DbmsDbms
Dbms
 
BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...
 
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
 
CS 542 Overview of query processing
CS 542 Overview of query processingCS 542 Overview of query processing
CS 542 Overview of query processing
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
SQL report
SQL reportSQL report
SQL report
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
 
Sql
SqlSql
Sql
 

More from Mithun Shanbhag

CloudSkew Architecture
CloudSkew ArchitectureCloudSkew Architecture
CloudSkew Architecture
Mithun Shanbhag
 
Azure Pipelines
Azure PipelinesAzure Pipelines
Azure Pipelines
Mithun Shanbhag
 
Terraform on Azure
Terraform on AzureTerraform on Azure
Terraform on Azure
Mithun Shanbhag
 
Identity, authentication and authorization
Identity, authentication and authorizationIdentity, authentication and authorization
Identity, authentication and authorization
Mithun Shanbhag
 
Design Patterns for Data Management and Consistency
Design Patterns for Data Management and ConsistencyDesign Patterns for Data Management and Consistency
Design Patterns for Data Management and Consistency
Mithun Shanbhag
 
WSL - Windows SubSytem For Linux
WSL - Windows SubSytem For LinuxWSL - Windows SubSytem For Linux
WSL - Windows SubSytem For Linux
Mithun Shanbhag
 
Creating user-mode debuggers for Windows
Creating user-mode debuggers for WindowsCreating user-mode debuggers for Windows
Creating user-mode debuggers for Windows
Mithun Shanbhag
 
Crash course in git and github
Crash course in git and githubCrash course in git and github
Crash course in git and github
Mithun Shanbhag
 

More from Mithun Shanbhag (8)

CloudSkew Architecture
CloudSkew ArchitectureCloudSkew Architecture
CloudSkew Architecture
 
Azure Pipelines
Azure PipelinesAzure Pipelines
Azure Pipelines
 
Terraform on Azure
Terraform on AzureTerraform on Azure
Terraform on Azure
 
Identity, authentication and authorization
Identity, authentication and authorizationIdentity, authentication and authorization
Identity, authentication and authorization
 
Design Patterns for Data Management and Consistency
Design Patterns for Data Management and ConsistencyDesign Patterns for Data Management and Consistency
Design Patterns for Data Management and Consistency
 
WSL - Windows SubSytem For Linux
WSL - Windows SubSytem For LinuxWSL - Windows SubSytem For Linux
WSL - Windows SubSytem For Linux
 
Creating user-mode debuggers for Windows
Creating user-mode debuggers for WindowsCreating user-mode debuggers for Windows
Creating user-mode debuggers for Windows
 
Crash course in git and github
Crash course in git and githubCrash course in git and github
Crash course in git and github
 

Recently uploaded

LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 

Recently uploaded (20)

LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 

Crash course in sql

  • 1. Crash course in SQL www.skewcode.com
  • 2. MySQL - Installation DOWNLOAD ○ https://dev.mysql.com/downloads ■ MySQL Community Server ■ MySQL Shell ■ [OPTIONAL] MySQL Workbench ○ https://dev.mysql.com/doc/index-other.html ■ Example databases -> world database
  • 3. Data Types Data types ● Numeric types (INT, DECIMAL, FLOAT, DOUBLE, BIT) ● Date & Time types (DATE, TIME, DATETIME, TIMESTAMP) ● String types (CHAR, VARCHAR, TEXT, ENUM)
  • 4. Data Definition Statements Database related operations List all databases SHOW DATABASES; Create a new database. CREATE DATABASE mydb1; Create a new database (if not existing). CREATE DATABASE IF NOT EXISTS mydb1; Use an existing database. USE mydb1; List current database. SELECT DATABASE(); Delete existing database. DROP DATABASE mydb1; Delete existing database (if existing). DROP DATABASE IF EXISTS mydb1;
  • 5. Data Definition Statements Table related operations List all tables in current database SHOW TABLES; See structure of a specific table. DESCRIBE mytable1; Create a new table (if not existing). CREATE TABLE IF NOT EXISTS mytable1 ( serialnum INT, name VARCHAR(20), location ENUM(‘BLR’, ‘BOM’, ‘DEL’) ); Delete existing table (if existing). DROP TABLE IF EXISTS mytable1;
  • 6. Data Definition Statements Table related operations Adding columns. ALTER TABLE mytable1 ADD COLUMN firstname varchar(25), ADD COLUMN lastname varchar(40); Deleting columns. ALTER TABLE mytable1 DROP COLUMN firstname, DROP COLUMN lastname; Modifying columns. ALTER TABLE mytable1 MODIFY COLUMN firstname varchar(25), MODIFY COLUMN lastname varchar(40);
  • 7. Data Definition Statements Constraints NOT NULL CREATE TABLE mytable1 (col1 INT NOT NULL); PRIMARY KEY CREATE TABLE mytable1 (col1 INT, PRIMARY KEY (col1)); UNIQUE CREATE TABLE mytable1 (col1 INT UNIQUE); FOREIGN KEY CREATE TABLE mytable1 ( col1 INT, FOREIGN KEY(col1) REFERENCES mytable2(mycol) ); DEFAULT CREATE TABLE mytable1 (col1 INT DEFAULT 100);
  • 8. Data Manipulation Statements Adding, deleting and updating data (rows) Add a new row (data in all columns). INSERT INTO mytable1 VALUES (‘10’, ‘mithun’, ‘shanbhag’); Add a new row (data in specific columns). INSERT INTO mytable1 (firstname, lastname) VALUES (‘mithun’, ‘shanbhag’); Update row(s). UPDATE mytable1 SET firstname = ‘sachin’, lastname = ‘tendulkar’ WHERE serialnum = 101; Delete row(s). DELETE FROM mytable1 WHERE firstname = ‘mithun’; Delete all row(s). DELETE * FROM mytable1;
  • 9. Data Query Statements ● Data Query Statements ○ Filtering Operations (WHERE) ○ Sorting Operations (ORDER BY) ○ Set Operations (DISTINCT, UNION, UNION ALL) ○ Paging Operations (LIMIT) ○ Aliasing Operations (AS) ○ Aggregation Operations (MIN, MAX, AVERAGE, SUM, COUNT) ○ Grouping Operations (GROUP BY) ○ Join Operations (JOIN)
  • 10. Data Query Statements Filtering Operations (WHERE) Extract cities in USA. SELECT * FROM city WHERE countrycode = ’usa’; Extract cities in USA with population greater than 1,000,000. SELECT * FROM city WHERE countrycode = ’usa’ AND population > 1000000; Extract cities in USA or cities with population greater than 1,000,000. SELECT * FROM city WHERE countrycode = ‘usa’ OR population > 1000000;
  • 11. Data Query Statements Filtering Operations (WHERE) Extract countries starting with ‘B’. SELECT * FROM country WHERE name LIKE ’b%’; Extract countries starting with ‘B’ and ending with ‘a’. SELECT * FROM country WHERE name LIKE ’b%a’; Extract countries which have 5 letters in their name. SELECT * FROM country WHERE name LIKE ’_____’;
  • 12. Data Query Statements Filtering Operations (WHERE) Extract countries with no head of state. SELECT * FROM country WHERE headofstate IS NULL; Extract countries which have heads of state. SELECT * FROM country WHERE headofstate IS NOT NULL; Extract countries beginning from letter ‘W’ onwards. SELECT * FROM country WHERE name > ’w’; Extract countries with population between 10,000 and 20,000. SELECT * FROM country WHERE population BETWEEN 10000 AND 20000;
  • 13. Data Query Statements Sorting Operations (ORDER BY) Sort countries by name. SELECT * FROM country ORDER BY name; Sort countries by population (descending). SELECT * FROM country ORDER BY population DESC; Sort countries by continent, then by population (descending). SELECT * FROM country ORDER BY continent, population DESC;
  • 14. Data Query Statements Set Operations (DISTINCT) Extract unique continent names SELECT DISTINCT continent FROM country; Extract unique countrycode and district combinations SELECT DISTINCT countrycode, district FROM city;
  • 15. Data Query Statements Set Operations (UNION) ● Combines the result-set of two or more SELECT statements. ● Each SELECT statement must have the same number of columns, with ‘similar’ data types and in the same order. ● UNION supports only ‘distinct’ values, however UNION ALL allows duplicates. Combine the list of country codes from the ‘city’ and ‘countrylanguage’ tables. SELECT countrycode FROM city UNION SELECT countrycode FROM countrylanguage ORDER BY countrycode;
  • 16. Data Query Statements Paging Operations (LIMIT) ● Constrain the number of rows returned in result set. Extract top 5 countries, sorted by population. SELECT * FROM country ORDER BY population DESC LIMIT 5; Extract countries 6 to 10, sorted by population. SELECT * FROM country ORDER BY population DESC LIMIT 5, 5;
  • 17. Data Query Statements Aliasing Operations (AS) ● Gives a table (or column) a temporary, readable name (alias). ● The alias exists only for the duration of the query. Aliasing a column name. SELECT name AS cityname, population FROM country WHERE cityname = ‘new york’; Aliasing a table name. SELECT name, population FROM country as c WHERE c.name = ‘new york’;
  • 18. Data Query Statements Aggregation Operations (MIN, MAX, AVG, SUM, COUNT) Least population. SELECT MIN(population) FROM country; Most population. SELECT MAX(population) FROM country; Average population. SELECT AVG(population) FROM country; Total population. SELECT SUM(population) FROM country; Total number of countries. SELECT COUNT(name) FROM country;
  • 19. Data Query Statements Grouping Operations (GROUP BY) ● Groups the result set using a ‘key’. ● The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG). Find number of countries in each continent. SELECT continent, count(name) FROM country GROUP BY continent; Group Swedish districts by total population. SELECT district, sum(population) FROM city WHERE countrycode = ‘swe’ GROUP BY district;
  • 20. Data Query Statements Join Operations INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
  • 21. Data Query Statements Inner Join Display continent information for cities. SELECT id, city.name, countrycode, district, continent FROM city INNER JOIN country ON city.CountryCode = country.code;

Editor's Notes

  1. System preferences -> mYSql -> enable server PATH=$PATH:/usr/local/mysql/bin
  2. MySQL does not support CHECK constraints? INDEX has been purposely left out
  3. SQL UPDATE - Don’t forget the WHERE clause, else all rows will be updated.
  4. String comparison is case insensitive Comparison operators on string, numbers, date Collation, character sets
  5. String comparison is case insensitive Comparison operators on string, numbers, date Collation, character sets
  6. String comparison is case insensitive Comparison operators on string, numbers, date Collation, character sets
  7. Need a better example.
  8. Question: How will you do countries 11 - 20 in this list?
  9. Database.table.column. We’ve not covered dot notations yet.
  10. GROUP BY DESC