SlideShare a Scribd company logo
MySQL Basics
Jamshid Hashimi
Trainer, Cresco Solution
http://www.jamshidhashimi.com
jamshid@netlinks.af
@jamshidhashimi
ajamshidhashimi
Afghanistan Workforce
Development Program
Agenda
• Introduction MySQL
• Simple Select
• MySQL Connect
• Comments
• Whitespace and Semi-colons
• Case Sensitivity
• SELECTing All Columns in All Rows
• SELECTing Specific Columns
• Sorting Records
• Sorting By a Single Column
• Sorting By Multiple Columns
• Sorting By Column Position
Agenda
• Ascending and Descending Sorts
• The WHERE Clause and Operator Symbols
• Checking for Equality
• Checking for Inequality
• Checking for Greater or Less Than
• Checking for NULL
• The WHERE Clause and Operator Words
• The BETWEEN Operator
• The IN Operator
• The LIKE Operator
• The NOT Operator
• Checking Multiple Conditions
• AND, OR, Order of Evaluation
Introduction MySQL
• MySQL is a fast, easy-to-use RDBMS used being used for many small
and big businesses. MySQL was owned and sponsored by a single
for-profit firm, the Swedish company MySQL AB, now owned by
Oracle Corporation. MySQL is becoming so popular because of
many good reasons.
• MySQL is released under an open-source license. So you have
nothing to pay to use it.
• MySQL is a very powerful program in its own right. It handles a
large subset of the functionality of the most expensive and
powerful database packages.
• MySQL uses a standard form of the well-known SQL data language.
• MySQL works on many operating systems and with many languages
including PHP, PERL, C, C++, JAVA etc.
Introduction to MySQL
• MySQL works very quickly and works well even with
large data sets.
• MySQL is very friendly to PHP, the most appreciated
language for web development.
• MySQL supports large databases, up to 50 million rows
or more in a table. The default file size limit for a table
is 4GB, but you can increase this (if your operating
system can handle it) to a theoretical limit of 8 million
terabytes (TB).
• MySQL is customizable. The open source GPL license
allows programmers to modify the MySQL software to
fit their own specific environments.
Simple SELECT
• A query is a question or a request
SELECT last_name FROM employees
MySQL Connect
• Use the PHP mysqli_connect() function to
open a new connection to the MySQL server.
mysqli_connect(host,username,password,dbname);
MySQL Connect
<?php
$connection = mysqli_connect("localhost","root","","awd_db");
// Check connection
if (mysqli_connect_errno($connection))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($connection);
Comments
# this is a comment
-- This is also a comment
/*
This
is a
comment
*/
SELECT * FROM cms_news;
Whitespace and Semi-colons
DEMO
Case Sensitivity
• Although database, table, and trigger names
are not case sensitive on some platforms, you
should not refer to one of these using
different cases within the same statement.
SELECT * FROM my_table WHERE MY_TABLE.col=1;
Case Sensitivity
• Column, index, stored routine, and event
names are not case sensitive on any platform,
nor are column aliases.
• By default, table aliases are case sensitive on
Unix, but not so on Windows or Mac OS X. The
following statement would not work on Unix,
because it refers to the alias both as a and as
A:
SELECT col_name FROM tbl_name AS a WHERE
a.col_name = 1 OR A.col_name = 2;
SELECTing All Columns in All Rows
• SELECT Syntax
SELECT expressions_and_columns FROM table_name
[WHERE some_condition_is_true]
[ORDER BY some_column [ASC | DESC]]
[LIMIT offset, rows]
SELECT * FROM tbl_name;
SELECTing Specific Columns
• If you do not want to see entire rows from
your table, just name the columns in which
you are interested, separated by commas.
SELECT name_id, firstname, lastname FROM
tbl_name;
Sorting Records
• By default, results of SELECT queries are
ordered as they appear in the table. If you
want to order results a specific way, such as by
date, ID, name, and so on, specify your
requirements using the ORDER BY clause.
SELECT name_id, firstname, lastname FROM
tbl_name ORDER BY lastname;
Sorting By a Single Column
• The default sorting of ORDER BY results is
ascending (ASC); strings sort from A to Z,
integers start at 0, dates sort from oldest to
newest. You can also specify a descending
sort, using DESC.
SELECT name_id, firstname, lastname FROM
tbl_name ORDER BY lastname DESC;
Sorting By Multiple Columns
• You're not limited to sorting by just one
field—you can specify as many fields as you
want, separated by commas. The sorting
priority is by list order, so if you use ORDER BY
lastname, firstname, the results will be sorted
by lastname, then by firstname.
SELECT name_id, firstname, lastname FROM
tbl_name ORDER BY lastname, firstname;
Sorting by Column Position
• Columns selected for output can be referred
to in ORDER BY and GROUP BY clauses using
column names, column aliases, or column
positions. Column positions are integers and
begin with 1
SELECT name_id, firstname, lastname FROM
tbl_name ORDER BY 2;
The WHERE Clause and Operator
Symbols
• We can use a conditional clause called WHERE clause
to filter out results. Using WHERE clause we can
specify a selection criteria to select required records
from a table.
• You can use one or more tables separated by comma
to include various condition using a WHERE clause. But
WHERE clause is an optional part of SELECT command.
• You can specify any condition using WHERE clause.
• You can specify more than one conditions using AND or
OR operators.
• A WHERE clause can be used along with DELETE or
UPDATE SQL command also to specify a condition.
The WHERE Clause and Operator
Symbols
The WHERE Clause and Operator
Symbols
SELECT * FROM cms_news WHERE `id` != 1;
SELECT * FROM cms_news WHERE `id` >= 1;
Checking for Equality
DEMO
Checking for Inequality
DEMO
Checking for Greater or Less Than
DEMO
Checking for NULL
• IS NULL: operator returns true of column value
is NULL.
• IS NOT NULL: operator returns true of column
value is not NULL.
SELECT * FROM cms_news WHERE title IS NULL;
SELECT * FROM cms_news WHERE title IS NOT
NULL;
The WHERE Clause and Operator
Words
SELECT * FROM cms_news WHERE `id` = 1 OR `id` = 2;
SELECT * FROM cms_news WHERE `id` = 1 AND
`status` = "a”;
The BETWEEN Operator
• The BETWEEN operator allows you to specify a
range to test.
SELECT productCode,
productName,
buyPrice
FROM products
WHERE buyPrice BETWEEN 90 AND 100
The IN Operator
• The IN operator allows you to determine if a
specified value matches any one of a list or a
subquery.
SELECT column_list
FROM table_name
WHERE (expr|column) IN
('value1','value2',...)
SELECT officeCode, city, phone
FROM offices
WHERE country IN ('USA','France')
The LIKE Operator
• The MySQL LIKE operator is commonly used to
select data based on patterns matching. Using
the LIKE operator in appropriate way is
essential to increase the query performance.
– The percentage ( %) wildcard allows you to match
any string of zero or more characters.
– The underscore (_) wildcard allows you to match
any single character.
The LIKE Operator
SELECT employeeNumber, lastName, firstName
FROM employees
WHERE firstName LIKE 'a%'
SELECT employeeNumber, lastName, firstName
FROM employees
WHERE lastname LIKE '%on%'
SELECT employeeNumber, lastName, firstName
FROM employees
WHERE firstname LIKE 'T_m'
DEMO
QUESTIONS?

More Related Content

PPT
Oracle Course
PPTX
Oracle basic queries
PDF
MySQL: Indexing for Better Performance
PPTX
Lab5 sub query
PPTX
Oracle Database View
PDF
TSQL Coding Guidelines
PDF
3 indexes
PPT
Subqueries -Oracle DataBase
Oracle Course
Oracle basic queries
MySQL: Indexing for Better Performance
Lab5 sub query
Oracle Database View
TSQL Coding Guidelines
3 indexes
Subqueries -Oracle DataBase

What's hot (20)

PPT
SQL WORKSHOP::Lecture 6
PDF
SQA server performance tuning
PPTX
XML Business Rules Validation with Schematron
PPTX
MySQL Performance Tips & Best Practices
PDF
Database development coding standards
PDF
Goldilocks and the Three MySQL Queries
PPT
Sql 2006
PPTX
Indexing the MySQL Index: Key to performance tuning
PPT
Creating other schema objects
PPTX
Episode 6 - DML, Transaction and Error handling in Salesforce
PPT
Creating Views - oracle database
PPT
Oracle SQL, PL/SQL best practices
PDF
How to analyze and tune sql queries for better performance percona15
PPT
Tunning overview
PPTX
Sql killedserver
PDF
B+Tree Indexes and InnoDB
PPT
SQL Pass Through and the ODBC Interface
PDF
Extensible Data Modeling
PPT
Sql views
PPT
Sql Pass Through
SQL WORKSHOP::Lecture 6
SQA server performance tuning
XML Business Rules Validation with Schematron
MySQL Performance Tips & Best Practices
Database development coding standards
Goldilocks and the Three MySQL Queries
Sql 2006
Indexing the MySQL Index: Key to performance tuning
Creating other schema objects
Episode 6 - DML, Transaction and Error handling in Salesforce
Creating Views - oracle database
Oracle SQL, PL/SQL best practices
How to analyze and tune sql queries for better performance percona15
Tunning overview
Sql killedserver
B+Tree Indexes and InnoDB
SQL Pass Through and the ODBC Interface
Extensible Data Modeling
Sql views
Sql Pass Through
Ad

Viewers also liked (9)

PPT
DBMS basics
PPT
DBMS an Example
PDF
Chapter 1 Fundamentals of Database Management System
PDF
CBSE XII Database Concepts And MySQL Presentation
PPT
Database Management Systems (DBMS)
DOCX
Database management system
PPT
Data Base Management System
PPT
Basic DBMS ppt
PPTX
Dbms slides
DBMS basics
DBMS an Example
Chapter 1 Fundamentals of Database Management System
CBSE XII Database Concepts And MySQL Presentation
Database Management Systems (DBMS)
Database management system
Data Base Management System
Basic DBMS ppt
Dbms slides
Ad

Similar to MySQL basics (20)

PDF
SQL Lesson 6 - Select.pdf
PPSX
MS SQL Server
PPTX
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
PPTX
SQL LECTURE.pptx
PDF
Sql server 2016 queries
PPTX
Subqueries, Backups, Users and Privileges
PPTX
Practical 03 (1).pptx
PDF
IR SQLite Session #1
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PDF
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
PPTX
DB2 Sql Query
PPTX
SQL.pptx for the begineers and good know
PDF
DP080_Lecture_1 SQL lecture document .pdf
PDF
Fortify aws aurora_proxy_2019_pleu
PPTX
Introduction to mysql part 3
PPT
INTRODUCTION TO SQL QUERIES REALTED BRIEF
PDF
Optimizer overviewoow2014
PPTX
Beginers guide for oracle sql
PPTX
Day 6.pptx
PDF
Structure query language, database course
SQL Lesson 6 - Select.pdf
MS SQL Server
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
SQL LECTURE.pptx
Sql server 2016 queries
Subqueries, Backups, Users and Privileges
Practical 03 (1).pptx
IR SQLite Session #1
Chapter – 6 SQL Lab Tutorial.pdf
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
DB2 Sql Query
SQL.pptx for the begineers and good know
DP080_Lecture_1 SQL lecture document .pdf
Fortify aws aurora_proxy_2019_pleu
Introduction to mysql part 3
INTRODUCTION TO SQL QUERIES REALTED BRIEF
Optimizer overviewoow2014
Beginers guide for oracle sql
Day 6.pptx
Structure query language, database course

More from Jamshid Hashimi (20)

PPTX
Week 2: Getting Your Hands Dirty – Part 2
PPTX
Week 1: Getting Your Hands Dirty - Part 1
PPTX
Introduction to C# - Week 0
PPTX
RIST - Research Institute for Science and Technology
PPTX
How Coding Can Make Your Life Better
PPTX
Mobile Vision
PPTX
Tips for Writing Better Code
PPTX
Launch Your Local Blog & Social Media Integration
PPTX
Customizing Your Blog 2
PPTX
Customizing Your Blog 1
PPTX
Introduction to Blogging
PPTX
Introduction to Wordpress
PPTX
CodeIgniter Helper Functions
PPTX
CodeIgniter Class Reference
PPTX
Managing Applications in CodeIgniter
PPTX
CodeIgniter Practice
PPTX
CodeIgniter & MVC
PPTX
PHP Frameworks & Introduction to CodeIgniter
PPTX
Exception & Database
PPTX
MySQL Record Operations
Week 2: Getting Your Hands Dirty – Part 2
Week 1: Getting Your Hands Dirty - Part 1
Introduction to C# - Week 0
RIST - Research Institute for Science and Technology
How Coding Can Make Your Life Better
Mobile Vision
Tips for Writing Better Code
Launch Your Local Blog & Social Media Integration
Customizing Your Blog 2
Customizing Your Blog 1
Introduction to Blogging
Introduction to Wordpress
CodeIgniter Helper Functions
CodeIgniter Class Reference
Managing Applications in CodeIgniter
CodeIgniter Practice
CodeIgniter & MVC
PHP Frameworks & Introduction to CodeIgniter
Exception & Database
MySQL Record Operations

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
 
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
 
PDF
KodekX | Application Modernization Development
 
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
 
CIFDAQ's Market Insight: SEC Turns Pro Crypto
 
KodekX | Application Modernization Development
 
Encapsulation theory and applications.pdf
NewMind AI Monthly Chronicles - July 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf

MySQL basics

  • 1. MySQL Basics Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
  • 2. Agenda • Introduction MySQL • Simple Select • MySQL Connect • Comments • Whitespace and Semi-colons • Case Sensitivity • SELECTing All Columns in All Rows • SELECTing Specific Columns • Sorting Records • Sorting By a Single Column • Sorting By Multiple Columns • Sorting By Column Position
  • 3. Agenda • Ascending and Descending Sorts • The WHERE Clause and Operator Symbols • Checking for Equality • Checking for Inequality • Checking for Greater or Less Than • Checking for NULL • The WHERE Clause and Operator Words • The BETWEEN Operator • The IN Operator • The LIKE Operator • The NOT Operator • Checking Multiple Conditions • AND, OR, Order of Evaluation
  • 4. Introduction MySQL • MySQL is a fast, easy-to-use RDBMS used being used for many small and big businesses. MySQL was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now owned by Oracle Corporation. MySQL is becoming so popular because of many good reasons. • MySQL is released under an open-source license. So you have nothing to pay to use it. • MySQL is a very powerful program in its own right. It handles a large subset of the functionality of the most expensive and powerful database packages. • MySQL uses a standard form of the well-known SQL data language. • MySQL works on many operating systems and with many languages including PHP, PERL, C, C++, JAVA etc.
  • 5. Introduction to MySQL • MySQL works very quickly and works well even with large data sets. • MySQL is very friendly to PHP, the most appreciated language for web development. • MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoretical limit of 8 million terabytes (TB). • MySQL is customizable. The open source GPL license allows programmers to modify the MySQL software to fit their own specific environments.
  • 6. Simple SELECT • A query is a question or a request SELECT last_name FROM employees
  • 7. MySQL Connect • Use the PHP mysqli_connect() function to open a new connection to the MySQL server. mysqli_connect(host,username,password,dbname);
  • 8. MySQL Connect <?php $connection = mysqli_connect("localhost","root","","awd_db"); // Check connection if (mysqli_connect_errno($connection)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_close($connection);
  • 9. Comments # this is a comment -- This is also a comment /* This is a comment */ SELECT * FROM cms_news;
  • 11. Case Sensitivity • Although database, table, and trigger names are not case sensitive on some platforms, you should not refer to one of these using different cases within the same statement. SELECT * FROM my_table WHERE MY_TABLE.col=1;
  • 12. Case Sensitivity • Column, index, stored routine, and event names are not case sensitive on any platform, nor are column aliases. • By default, table aliases are case sensitive on Unix, but not so on Windows or Mac OS X. The following statement would not work on Unix, because it refers to the alias both as a and as A: SELECT col_name FROM tbl_name AS a WHERE a.col_name = 1 OR A.col_name = 2;
  • 13. SELECTing All Columns in All Rows • SELECT Syntax SELECT expressions_and_columns FROM table_name [WHERE some_condition_is_true] [ORDER BY some_column [ASC | DESC]] [LIMIT offset, rows] SELECT * FROM tbl_name;
  • 14. SELECTing Specific Columns • If you do not want to see entire rows from your table, just name the columns in which you are interested, separated by commas. SELECT name_id, firstname, lastname FROM tbl_name;
  • 15. Sorting Records • By default, results of SELECT queries are ordered as they appear in the table. If you want to order results a specific way, such as by date, ID, name, and so on, specify your requirements using the ORDER BY clause. SELECT name_id, firstname, lastname FROM tbl_name ORDER BY lastname;
  • 16. Sorting By a Single Column • The default sorting of ORDER BY results is ascending (ASC); strings sort from A to Z, integers start at 0, dates sort from oldest to newest. You can also specify a descending sort, using DESC. SELECT name_id, firstname, lastname FROM tbl_name ORDER BY lastname DESC;
  • 17. Sorting By Multiple Columns • You're not limited to sorting by just one field—you can specify as many fields as you want, separated by commas. The sorting priority is by list order, so if you use ORDER BY lastname, firstname, the results will be sorted by lastname, then by firstname. SELECT name_id, firstname, lastname FROM tbl_name ORDER BY lastname, firstname;
  • 18. Sorting by Column Position • Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1 SELECT name_id, firstname, lastname FROM tbl_name ORDER BY 2;
  • 19. The WHERE Clause and Operator Symbols • We can use a conditional clause called WHERE clause to filter out results. Using WHERE clause we can specify a selection criteria to select required records from a table. • You can use one or more tables separated by comma to include various condition using a WHERE clause. But WHERE clause is an optional part of SELECT command. • You can specify any condition using WHERE clause. • You can specify more than one conditions using AND or OR operators. • A WHERE clause can be used along with DELETE or UPDATE SQL command also to specify a condition.
  • 20. The WHERE Clause and Operator Symbols
  • 21. The WHERE Clause and Operator Symbols SELECT * FROM cms_news WHERE `id` != 1; SELECT * FROM cms_news WHERE `id` >= 1;
  • 24. Checking for Greater or Less Than DEMO
  • 25. Checking for NULL • IS NULL: operator returns true of column value is NULL. • IS NOT NULL: operator returns true of column value is not NULL. SELECT * FROM cms_news WHERE title IS NULL; SELECT * FROM cms_news WHERE title IS NOT NULL;
  • 26. The WHERE Clause and Operator Words SELECT * FROM cms_news WHERE `id` = 1 OR `id` = 2; SELECT * FROM cms_news WHERE `id` = 1 AND `status` = "a”;
  • 27. The BETWEEN Operator • The BETWEEN operator allows you to specify a range to test. SELECT productCode, productName, buyPrice FROM products WHERE buyPrice BETWEEN 90 AND 100
  • 28. The IN Operator • The IN operator allows you to determine if a specified value matches any one of a list or a subquery. SELECT column_list FROM table_name WHERE (expr|column) IN ('value1','value2',...) SELECT officeCode, city, phone FROM offices WHERE country IN ('USA','France')
  • 29. The LIKE Operator • The MySQL LIKE operator is commonly used to select data based on patterns matching. Using the LIKE operator in appropriate way is essential to increase the query performance. – The percentage ( %) wildcard allows you to match any string of zero or more characters. – The underscore (_) wildcard allows you to match any single character.
  • 30. The LIKE Operator SELECT employeeNumber, lastName, firstName FROM employees WHERE firstName LIKE 'a%' SELECT employeeNumber, lastName, firstName FROM employees WHERE lastname LIKE '%on%' SELECT employeeNumber, lastName, firstName FROM employees WHERE firstname LIKE 'T_m'
  • 31. DEMO