SlideShare a Scribd company logo
SQL Select
SQL Select
 The SELECT statement is used to pull information from a
table.
 SELECT retrieves rows, columns, and derived values
from one or more tables.
 The general format is:
Syntax:
SELECT column(s)
FROM table(s)
[JOIN join(s)]
[WHERE search_condition(s)]
[GROUPBY grouping_column(s)]
[HAVING search_condition(s)]
[ORDERBY sort_column(s)];
Selecting All Data
 The simplest form of SELECT retrieves everything from a
table
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1999-02-04 | NULL |
| Claws | Gwen | cat | f | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1999-08-27 | NULL |
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)
Selecting Particular Rows
 You can select only particular rows from your table.
 For example, if you want to verify the change that you
made to Bowser's birth date, select Bowser's record like
this:
mysql> SELECT * FROM students WHERE name = “Ali";
+--------+-------+------+------------+
| name | owner | sex | birth |
+--------+-------+------+------------+
| Ali | Ahmed | m | 1998-08-31 |
+--------+-------+------+------------+
1 row in set (0.00 sec)
Selecting Particular Rows
 To find all students born after 1998
SELECT * FROM students WHERE birth >= "1998-1-1";
 To find all employees where department IT And
gender male.
SELECT * FROM employees WHERE dept_id=7 AND gender = “male";
 To find all employees having department id 2 or 7,
use a logical OR
SELECT * FROM employees WHERE dept_id=2
OR dept_id=7;
Selecting Particular Columns
 If you don’t want to see entire rows from your table, just
name the columns in which you are interested, separated
by commas.
 mysql> select name, cnic_no from employees;
+----------+-----------------+
| name | cnic_no |
+----------+-----------------+
| Ahmed | 41306-4076076-6 |
| Ali | 31306-4076870-7 |
| Faisal | 41306-6034500-4 |
| Sadaf | 47606-6476000-7 |
| Yousuf | 69876-4076000-2 |
| Muhammad | 46577-4076000-5 |
| Sehar | 42234-5676000-4 |
| Sobia | 57306-4076000-6 |
+----------+-----------------+
8 rows in set (0.01 sec)
AS
 The AS statement can be used to create a column alias
(an alternative name/identifier) that you specify to
control how column headings are displayed in a result.
 Syntax:
 SELECT column1 AS alias1,
 column2 AS alias2,
 ...
 columnN ASaliasN
 FROMtable;
AS Example
 To rename column alias
 SELECT nemes AS ‘Employee Name’,birth AS ‘Birth Date’ FROM employees;
 +---------------+------------+
 | Employee Name | Birth Date |
 +---------------+------------+
 | Ahmed | 1991-08-27 |
 | Yusuf | 1990-02-04 |
 | Sehar | 1990-09-11 |
 | Sobia | 1988-08-31 |
 | Muhammad | 1988-12-09 |
 | Faisal | 1987-04-29 |
 | Ali | 1985-03-17 |
 | Sadaf | 1983-05-13 |
 +---------------+------------+
 8 rows in set (0.02 sec)
DISTINCT
 Results of queries oftentimes contain duplicate values for
a particular column.
 The DISTINCT keyword eliminates duplicate rows from
a result.
 Syntax:
 SELECT DISTINCT column(s)
 FROM table(s);
DISTINCT Example
 SELECT DISTINCT dept_id
 FROM employees;
Where
 The WHERE clause can be used to filter unwanted rows
in a result (ie, yield a subset of all rows in the result with
a specified condition).
 Syntax:
 SELECT column(s)
 FROM table
 WHERE test_column operatorvalue;
Types of Conditions
Comparison Operators
Notes on WHERE
 Occasionally, you may need to specify multiple conditions
in a single WHERE clause.
 You can use the AND, OR or NOT operators to
combine two or more conditions into a compound
condition.
 AND, OR, and NOT operators are known as Boolean
operators; they are designed to work with “truth”
values: true, false, and unknown.
LIKE
 You can use the LIKE operator to retrieve partial
information for a character string (not numbers or
date/times) rather than an exact value.
 LIKE uses a pattern that values are matched against.
Pattern Matching
 MySQL provides:
 Standard SQL pattern matching.
 SQL Pattern matching:
 To perform pattern matching, use the LIKE or NOT LIKE comparison
operators
 By default, patterns are case insensitive.
 Special Characters:
 _ Used to match any single character.
 % Used to match an arbitrary number of characters.
Pattern Matching Example
 To find names beginning with ‘b’:
 mysql> SELECT name FROM students WHERE name LIKE "b%";
+--------+
| name |
+--------+
| Bushra |
| Benazir|
+--------+
Pattern Matching Example
 To find names ending with ‘dia’
 mysql> SELECT name FROM students WHERE name LIKE "%dia";
+--------+
| name |
+--------+
| Fadia |
| Sadia |
+--------+
Pattern Matching Example
 To find names containing a ‘a’:
 mysql> SELECT name FROM students WHERE name LIKE "%a%";
+--------+
| name |
+--------+
| Ali |
| Ahmed |
| Saher |
| Sadia |
| Sadia |
+--------+
Pattern Matching Example
 To find names containing exactly five characters, use the _
pattern character:
 mysql> SELECT * FROM pet WHERE name LIKE "_____";
+--------+
| name |
+--------+
| Ahmed |
| Saher |
| Sadia |
| Sadia |
+--------+
Regular Expression Matching
 The other type of pattern matching provided by MySQL
uses extended regular expressions.
 When you test for a match for this type of pattern, use
the REGEXP and NOT REGEXP operators (or RLIKE
and NOT RLIKE, which are synonyms).
Regular Expression Example
 To find names beginning with b, use ^ to match the
beginning of the name:
 mysql> SELECT name FROM employees WHERE name REGEXP "^b";
+--------+
| name |
+--------+
| Bushra |
| Benazir|
+--------+
Regular Expression Example
 To find names ending with ‘dia’, use ‘$’ to match the
end of the name:
 mysql> SELECT name FROM students WHERE name REGEXP "dia$";
+--------+
| name |
+--------+
| Fadia |
| Sadia |
+--------+
Working with NULLs
 NULL means missing value or unknown value.
 To test for NULL, you cannot use the arithmetic
comparison operators, such as =, < or <>.
 Rather, you must use the IS NULL and IS NOT NULL
operators instead.
 mysql> select name from employees where phone IS NOT NULL;
+--------+
| name |
+--------+
| Ahmed |
+--------+
1 row in set (0.01 sec)
BETWEEN
 Use the BETWEEN clause to determine whether a given value
falls within a specified range.
 BETWEEN works with character strings, numbers, and
date/times.
 The range contains a low and high value, separated by AND
(inclusive).
 You can negate a BETWEEN condition with NOT BETWEEN.
 Syntax:
 SELECT columns
 FROM table
 WHERE test_column BETWEEN
 low_value AND high value;
BETWEEN Example
 SELECT name
 FROM employees
 WHERE salary BETWEEN 30000 AND 50000

More Related Content

PPTX
Computer & it's components
PPTX
Group By, Having Clause and Order By clause
PPTX
PHP FUNCTIONS
PDF
Oracle SQL Basics
PPTX
Web Development
PPT
Advanced Sql Training
PDF
Fundamental Skills volleyball.pdf
PPT
Queue AS an ADT (Abstract Data Type)
Computer & it's components
Group By, Having Clause and Order By clause
PHP FUNCTIONS
Oracle SQL Basics
Web Development
Advanced Sql Training
Fundamental Skills volleyball.pdf
Queue AS an ADT (Abstract Data Type)

What's hot (20)

PPTX
Nested queries in database
PPT
1 - Introduction to PL/SQL
PPTX
PL/SQL Fundamentals I
PDF
SQL JOINS
PPTX
ODP
PPT
Sql operators & functions 3
PPT
Sql query [select, sub] 4
PPT
Aggregate functions
PPT
SQL subquery
PPTX
Basic SQL and History
PPTX
introdution to SQL and SQL functions
PDF
SQL BUILT-IN FUNCTION
PPTX
Aggregate function
PPT
Introduction to-sql
PPT
Sql ppt
PPTX
Lecture 4 sql {basics keys and constraints}
PPT
JavaScript Tutorial
PPTX
Relational Data Model Introduction
PPT
Retrieving data using the sql select statement
Nested queries in database
1 - Introduction to PL/SQL
PL/SQL Fundamentals I
SQL JOINS
Sql operators & functions 3
Sql query [select, sub] 4
Aggregate functions
SQL subquery
Basic SQL and History
introdution to SQL and SQL functions
SQL BUILT-IN FUNCTION
Aggregate function
Introduction to-sql
Sql ppt
Lecture 4 sql {basics keys and constraints}
JavaScript Tutorial
Relational Data Model Introduction
Retrieving data using the sql select statement
Ad

Similar to Sql select (20)

PPTX
SQL Data Manipulation language and DQL commands
PPT
Chinabankppt
PPT
Module03
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
PPT
ALL ABOUT SQL AND RDBMS
PPT
Diva10
PDF
Common Table Expressions in MariaDB 10.2
PDF
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
PPT
01 basic orders
PPT
PPT
PPT
Les01 (retrieving data using the sql select statement)
PPTX
Optimizing queries MySQL
PPT
Distributed databases systems-3-2015.ppt
DOC
Best sql plsql material
PDF
SQL on Linux and its uses and application.pdf
PPTX
Data Manipulation Language.pptx
PPT
My sql presentation
PDF
0808.pdf
PDF
0808.pdf
SQL Data Manipulation language and DQL commands
Chinabankppt
Module03
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
ALL ABOUT SQL AND RDBMS
Diva10
Common Table Expressions in MariaDB 10.2
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
01 basic orders
Les01 (retrieving data using the sql select statement)
Optimizing queries MySQL
Distributed databases systems-3-2015.ppt
Best sql plsql material
SQL on Linux and its uses and application.pdf
Data Manipulation Language.pptx
My sql presentation
0808.pdf
0808.pdf
Ad

More from Mudasir Syed (20)

PPT
Error reporting in php
PPT
Cookies in php lecture 2
PPT
Cookies in php lecture 1
PPTX
PPT
Reporting using FPDF
PPT
Oop in php lecture 2
PPT
Oop in php lecture 2
PPT
Filing system in PHP
PPT
Time manipulation lecture 2
PPT
Time manipulation lecture 1
PPT
Php Mysql
PPT
Adminstrating Through PHPMyAdmin
PPT
PHP mysql Sql
PPT
PHP mysql Mysql joins
PPTX
PHP mysql Introduction database
PPT
PHP mysql Installing my sql 5.1
PPT
PHP mysql Er diagram
PPT
PHP mysql Database normalizatin
PPT
PHP mysql Aggregate functions
PPT
Form validation with built in functions
Error reporting in php
Cookies in php lecture 2
Cookies in php lecture 1
Reporting using FPDF
Oop in php lecture 2
Oop in php lecture 2
Filing system in PHP
Time manipulation lecture 2
Time manipulation lecture 1
Php Mysql
Adminstrating Through PHPMyAdmin
PHP mysql Sql
PHP mysql Mysql joins
PHP mysql Introduction database
PHP mysql Installing my sql 5.1
PHP mysql Er diagram
PHP mysql Database normalizatin
PHP mysql Aggregate functions
Form validation with built in functions

Sql select

  • 2. SQL Select  The SELECT statement is used to pull information from a table.  SELECT retrieves rows, columns, and derived values from one or more tables.  The general format is: Syntax: SELECT column(s) FROM table(s) [JOIN join(s)] [WHERE search_condition(s)] [GROUPBY grouping_column(s)] [HAVING search_condition(s)] [ORDERBY sort_column(s)];
  • 3. Selecting All Data  The simplest form of SELECT retrieves everything from a table mysql> select * from pet; +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1999-02-04 | NULL | | Claws | Gwen | cat | f | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1999-08-27 | NULL | | Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+------------+ 8 rows in set (0.00 sec)
  • 4. Selecting Particular Rows  You can select only particular rows from your table.  For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this: mysql> SELECT * FROM students WHERE name = “Ali"; +--------+-------+------+------------+ | name | owner | sex | birth | +--------+-------+------+------------+ | Ali | Ahmed | m | 1998-08-31 | +--------+-------+------+------------+ 1 row in set (0.00 sec)
  • 5. Selecting Particular Rows  To find all students born after 1998 SELECT * FROM students WHERE birth >= "1998-1-1";  To find all employees where department IT And gender male. SELECT * FROM employees WHERE dept_id=7 AND gender = “male";  To find all employees having department id 2 or 7, use a logical OR SELECT * FROM employees WHERE dept_id=2 OR dept_id=7;
  • 6. Selecting Particular Columns  If you don’t want to see entire rows from your table, just name the columns in which you are interested, separated by commas.  mysql> select name, cnic_no from employees; +----------+-----------------+ | name | cnic_no | +----------+-----------------+ | Ahmed | 41306-4076076-6 | | Ali | 31306-4076870-7 | | Faisal | 41306-6034500-4 | | Sadaf | 47606-6476000-7 | | Yousuf | 69876-4076000-2 | | Muhammad | 46577-4076000-5 | | Sehar | 42234-5676000-4 | | Sobia | 57306-4076000-6 | +----------+-----------------+ 8 rows in set (0.01 sec)
  • 7. AS  The AS statement can be used to create a column alias (an alternative name/identifier) that you specify to control how column headings are displayed in a result.  Syntax:  SELECT column1 AS alias1,  column2 AS alias2,  ...  columnN ASaliasN  FROMtable;
  • 8. AS Example  To rename column alias  SELECT nemes AS ‘Employee Name’,birth AS ‘Birth Date’ FROM employees;  +---------------+------------+  | Employee Name | Birth Date |  +---------------+------------+  | Ahmed | 1991-08-27 |  | Yusuf | 1990-02-04 |  | Sehar | 1990-09-11 |  | Sobia | 1988-08-31 |  | Muhammad | 1988-12-09 |  | Faisal | 1987-04-29 |  | Ali | 1985-03-17 |  | Sadaf | 1983-05-13 |  +---------------+------------+  8 rows in set (0.02 sec)
  • 9. DISTINCT  Results of queries oftentimes contain duplicate values for a particular column.  The DISTINCT keyword eliminates duplicate rows from a result.  Syntax:  SELECT DISTINCT column(s)  FROM table(s);
  • 10. DISTINCT Example  SELECT DISTINCT dept_id  FROM employees;
  • 11. Where  The WHERE clause can be used to filter unwanted rows in a result (ie, yield a subset of all rows in the result with a specified condition).  Syntax:  SELECT column(s)  FROM table  WHERE test_column operatorvalue;
  • 14. Notes on WHERE  Occasionally, you may need to specify multiple conditions in a single WHERE clause.  You can use the AND, OR or NOT operators to combine two or more conditions into a compound condition.  AND, OR, and NOT operators are known as Boolean operators; they are designed to work with “truth” values: true, false, and unknown.
  • 15. LIKE  You can use the LIKE operator to retrieve partial information for a character string (not numbers or date/times) rather than an exact value.  LIKE uses a pattern that values are matched against.
  • 16. Pattern Matching  MySQL provides:  Standard SQL pattern matching.  SQL Pattern matching:  To perform pattern matching, use the LIKE or NOT LIKE comparison operators  By default, patterns are case insensitive.  Special Characters:  _ Used to match any single character.  % Used to match an arbitrary number of characters.
  • 17. Pattern Matching Example  To find names beginning with ‘b’:  mysql> SELECT name FROM students WHERE name LIKE "b%"; +--------+ | name | +--------+ | Bushra | | Benazir| +--------+
  • 18. Pattern Matching Example  To find names ending with ‘dia’  mysql> SELECT name FROM students WHERE name LIKE "%dia"; +--------+ | name | +--------+ | Fadia | | Sadia | +--------+
  • 19. Pattern Matching Example  To find names containing a ‘a’:  mysql> SELECT name FROM students WHERE name LIKE "%a%"; +--------+ | name | +--------+ | Ali | | Ahmed | | Saher | | Sadia | | Sadia | +--------+
  • 20. Pattern Matching Example  To find names containing exactly five characters, use the _ pattern character:  mysql> SELECT * FROM pet WHERE name LIKE "_____"; +--------+ | name | +--------+ | Ahmed | | Saher | | Sadia | | Sadia | +--------+
  • 21. Regular Expression Matching  The other type of pattern matching provided by MySQL uses extended regular expressions.  When you test for a match for this type of pattern, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms).
  • 22. Regular Expression Example  To find names beginning with b, use ^ to match the beginning of the name:  mysql> SELECT name FROM employees WHERE name REGEXP "^b"; +--------+ | name | +--------+ | Bushra | | Benazir| +--------+
  • 23. Regular Expression Example  To find names ending with ‘dia’, use ‘$’ to match the end of the name:  mysql> SELECT name FROM students WHERE name REGEXP "dia$"; +--------+ | name | +--------+ | Fadia | | Sadia | +--------+
  • 24. Working with NULLs  NULL means missing value or unknown value.  To test for NULL, you cannot use the arithmetic comparison operators, such as =, < or <>.  Rather, you must use the IS NULL and IS NOT NULL operators instead.  mysql> select name from employees where phone IS NOT NULL; +--------+ | name | +--------+ | Ahmed | +--------+ 1 row in set (0.01 sec)
  • 25. BETWEEN  Use the BETWEEN clause to determine whether a given value falls within a specified range.  BETWEEN works with character strings, numbers, and date/times.  The range contains a low and high value, separated by AND (inclusive).  You can negate a BETWEEN condition with NOT BETWEEN.  Syntax:  SELECT columns  FROM table  WHERE test_column BETWEEN  low_value AND high value;
  • 26. BETWEEN Example  SELECT name  FROM employees  WHERE salary BETWEEN 30000 AND 50000