M Taimoor Khan
Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs) 5) Mappings (ERDs) 6) Formulating queries (Relational algebra) 7) Implementing Schema 8) Built-in Functions 9) Extracting data 10) Working with Joins 11) Normalization 12) Improving performance 13) Advanced topics
9) Extracting Data o DML o SHOW, DESCRIBE o SELECT o Projecting on certain columns o Applying conditions on records o Other KEYWORDS / OPERATORS
Select Statement Maximum used command in DML Used not only to select certain rows but also the columns Also used for different forms of product, that is, different joins
Select Selecting rows from one or more tables SELECT {*|col_name[,….n]} FROM table_name Example (list all students) SELECT * FROM student;
PROJECT on certain columns SELECT stName, prName FROM student
Attribute Alias SELECT stName as ‘Student Name’, prName ‘Program’ FROM Student
Formatting reports Select stId, crCode, mTerm + sMrks ‘Total out of 50’ from enroll
Formatting reports SELECT stName + ‘ studies in ‘ + prName FROM student
Unique values The DISTINCT keyword is used to return only distinct (different) values
Select Distinct Just add a DISTINCT keyword to the SELECT statement SELECT DISTINCT column_name(s) FROM table_name
Select Distinct Q: Get the program names in which students are enrolled SELECT prName FROM Student
Q: Get the program names in which students are enrolled SELECT prName FROM Student SELECT DISTINCT prName FROM Student Select Distinct
The WHERE Clause We used names to limit columns, but rows cannot be named due to the dynamicity We limit the rows using conditions Condition comes after the WHERE clause Condition is applied on a single or multiple columns providing it with some particular value or set of values
Student table stuIDstuNamecitypostCodeSem AftabIslamabad NaveedLahore KamalKarachi FawadMardan ZamanRawal pindi AdnanAttock FaisalHazro650002
Example SELECT stuName, sem FROM student WHERE city=“islamabad”;
LIMIT It limits the number of records in the result set as per the request Examples SELECT * FROM student LIMIT 0, 100; SELECT stuID, stuName FROM student LIMIT 2, 5; SELECT stuID, stuName FROM student WHERE postCode > LIMIT 0, 10;
Examples
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 | | NULL | | Claws | Gwen | cat | f | | NULL | | Buffy | Harold | dog | f | | NULL | | Fang | Benny | dog | m | | NULL | | Bowser | Diane | dog | m | | | | Chirpy | Gwen | bird | f | | NULL | | Whistler | Gwen | bird | | | NULL | | Slim | Benny | snake | m | | NULL | rows in set (0.00 sec) 26
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 pet WHERE name = "Bowser"; | name | owner | species | sex | birth | death | | Bowser | Diane | dog | m | | | row in set (0.00 sec) 27
Selecting Particular Rows To find all animals born after 1998 SELECT * FROM pet WHERE birth >= " "; To find all female dogs, use a logical AND SELECT * FROM pet WHERE species = "dog" AND sex = "f"; To find all snakes or birds, use a logical OR SELECT * FROM pet WHERE species = "snake" OR species = "bird"; 28
Selecting Particular Columns mysql> select name, birth from pet; | name | birth | | Fluffy | | | Claws | | | Buffy | | | Fang | | | Bowser | | | Chirpy | | | Whistler | | | Slim | | rows in set (0.01 sec) 29
Sorting Data To sort a result, use an ORDER BY clause. For example, to view animal birthdays, sorted by date: 30 mysql> SELECT name, birth FROM pet ORDER BY birth; | name | birth | | Buffy | | | Claws | | | Slim | | | Whistler | | | Bowser | | | Chirpy | | | Fluffy | | | Fang | | rows in set (0.02 sec)
Sorting Data To sort in reverse order, add the DESC (descending keyword) 31 mysql> SELECT name, birth FROM pet ORDER BY birth DESC; | name | birth | | Fang | | | Fluffy | | | Chirpy | | | Bowser | | | Whistler | | | Slim | | | Claws | | | Buffy | | rows in set (0.02 sec)
Selecting Particular Columns mysql> select name, birth from pet; | name | birth | | Fluffy | | | Claws | | | Buffy | | | Fang | | | Bowser | | | Chirpy | | | Whistler | | | Slim | | rows in set (0.01 sec) 32
Sorting Data To sort a result, use an ORDER BY clause. For example, to view animal birthdays, sorted by date: 33 mysql> SELECT name, birth FROM pet ORDER BY birth; | name | birth | | Buffy | | | Claws | | | Slim | | | Whistler | | | Bowser | | | Chirpy | | | Fluffy | | | Fang | | rows in set (0.02 sec)
Sorting Data To sort in reverse order, add the DESC (descending keyword) 34 mysql> SELECT name, birth FROM pet ORDER BY birth DESC; | name | birth | | Fang | | | Fluffy | | | Chirpy | | | Bowser | | | Whistler | | | Slim | | | Claws | | | Buffy | | rows in set (0.02 sec)
9) Extracting Data o DML o SHOW, DESCRIBE o SELECT o Projecting on certain columns o Applying conditions on records o Other KEYWORDS / OPERATORS
Next Lecture SELECT with keywords