MySQL SQL for MySQL (I) Salim Mail Phone YM Blog : salim.sucipto@gmail.com : 0815-188-2384 : talim_bansal : http://salimsribasuki.wordpress.com MySQL - Query 1 Summary •Retrieving Data Using the SQL SELECT Statement •Restricting and Sorting Data •Using Single-Row Functions to Customize Output MySQL - Query 2 SELECT COMMON PATTERN SELECT FROM WHERE AND/OR GROUP BY HAVING ORDER BY <*, Field 1,….., Field n, Aggregate Function> < Table Name> <Condition> <If any additional condition> <Field 1,……, Field n> <Condition> <Field1,…., Field n> MySQL - Query 3 Retrieving Data Using the SQL SELECT Statement •Select All Columns Syntax Sample : SELECT * FROM <Table Name>; : SELECT * FROM ti3k_item_master •Select Specific Columns Syntax Sample : SELECT <Column Name, Column Name, Column Name,……Column Name> FROM <Table Name>; : SELECT item_id, item_name, item_uom FROM ti3k_item_master; •Use Arithmetic Operators MySQL - Query 4 Retrieving Data Using the SQL SELECT Statement •Understand Operator Precedence Precedence defines the order that Oracle uses when evaluating different operators in the same expression. Every operator has a predefined precedence. Oracle evaluates operators with a higher precedence before it evaluates operators with a lower precedence. Operators with equal precedence will be evaluated from left to right MySQL - Query 5 Restricting and Sorting Data •Write queries that contain a WHERE clause to limit the output retrieved Syntax Sample : Select <Column Name,……Column Name> From <Table Name> Where <Column Name or Logical Phrase>; : SELECT item_id, item_name, item_uom FROM ti3k_item_master Where item_uom ='Set'; •Write queries that contain an ORDER BY clause sort the output of a SELECT statement (Ascending or Descending) Sample : Select * from ti3k_item_master Order by item_uom asc; Sample : Select * from ti3k_item_master Order by item_id desc; MySQL - Query 6 Restricting and Sorting Data List the comparison operators and logical operators that are used in a WHERE clause MySQL - Query 7 Sample SELECT with WHERE using Comparison operators Exercise: select item_uom “UOM” from ti3k_item_master where item_code =‘DRL10’; select item_id, item_code, item_name, item_uom, remark From ti3k_item_master where item_id > 50 And item_uom =‘Each’; select * from ti3k_item_master where item_uom in (‘Sax’,’Set’) Order by item_uom, item_id; select * from ti3k_item_master where remark is null Or created_by = ‘Salim’; select * from ti3k_item_master where item_id between 10 and 20; MySQL - Query 8