You are on page 1of 9
vatawsase Management system (333/03) PRACTICAL - 7 All using operators to retrieve data from given tables 36] es SQL operators © Operators are nised inside an expression ar canditinn ta specify parti operations. + Operators are reserve words that are generally used with WHERE clause, © SQL operator can be divided into main six categories : 1. Anttemette Operators Relational Operators Logical Operators Range searching Operators Set Searching Operators (Character Operators Woperators A ay uid Rac A ve UG As table Aau2 sé asa Pel SQL al atuy operators 2a dtable U2 apply sé 82, Pel dH4d wLwALAl HEAL 23. Ul Ug 2 wHLAG table 2 wie tatletsti Ally. Table name > PERSON Understand the concepts of SQL operators and write que SR NANE ‘SURNAME —BDATE CITY SPI parikh 15-JAN-10 snagar 9.3 2 vihan Sagar -22-FEB-12 rangpur 7 3 shreeja parikh —22-JUL-10 rajkot 9 4 samay saxena _10-AUG-10 ahmedabad 8.3 5 tushar —parmar = 13-NOV-90 palanpur 6.3 6 aarush —parikh ——15-JAN-15. snagar 8.12 7 avani solanki 15-MAY-10 ahmedabad 73 8 misha sutariya 22-DEC-12 ahmedabad 5.5 9 parth parikh 15-SEP-95 rajkot 9.3 18 aarav solanki 11-SEP-10 snagar 9.3 22008 208 55200 1208 vatasase management system (5550/05) + Arithmetic Operators «Arithmetic operators can perform arithmetical operations on numeric operands, (Bact UI arith Al ASA). ‘© We can use various Arithmeric Operators on the data stored in the tables, aperator Wal se §€1 A operands el data type numeric + Different arithmetic operations are: + Addition = Subraction + Multiplication 7 Division % — Modulus ()__ Enclosed operation Some examples of arithmetic operators using table Dual Input command—» [SELECT 543, 5-3, 5*3, 5/3 FROM dual; output —> | 543 5-3 s*3. 5/3 2 15 1.66666667 Example: Display serial number, name and balance from table ‘person’ by adding 100 rupees in batance of every person. SELECT er,nane,balance “CURRENT BAL”, balancesi@0 "NEW BAL” wut commend» [eanect 2n2P= persons output —* | _5 Nave Comment BAL REM BAL i bhagya is00e ”"isi00 2 vinan ope se6100 3 shreeda 000 S108 3 tushar s3ee0 35100 6 aarush 22000-22300 3 avant 200 300 & misha soos ssaee 9 parth 5000 S100 30 sarav i200 i300 WPoint to note (The result of mathematical expression is displayed as a separated column. And it is temporary only means the result doesn’t affect the original table data.) PR vatawase Management system (3330/05) + Relational Operators (comparison operator) ‘Relational operators are used to compare one expression with other. + These relational operators compare expressions and returns one of three values: ‘True, False, and Unknown. An unknown is returned when comparison is performed with a nnll value Anaunknawn is treated like ac falco SQL supports following relational operators, Equals <=__| Less than or equal to Tess than >= | Greater than or equal to via Not equals Greater than | Example: Display details of persons whose balance is greater than 15000. Input command —p SELECT * FROM person WHERE balance > 15800; ‘output ——>| SR NAME SURNAME —BDATE CITY ser 5 tushar —parmar —13-NOV-98 palanpur 6.3 6 aarush —parikh —15-JAN-15 snagar 8.12 @misha —sutariya 22-DEC-12 ahnedabad 5.5 + Logival uperaturs + SQL provides three logical operators > AND, OR and NOT. 2 These uperaturs Compare two Conditions at a Gime to deleniaine whetlier a row eat bbe selected for the output. Logical AND compares between two Booleans as AND | expression andl setumns True wher butl expressions are true. False otherwise. Logical OR compares between two Booleans as OR — expression and returns True when one of the expression is true, False otherwise. Not takes a single Boolean as an argument and changes its value from fils fo true or from true to filse. vatawsase Management system (333/03) AND operator ‘© Logical AND compares between two Booleans as expression and returns True when both expressions are true. False otherwise ‘+ AND operator is used to combine two or more conditions, It is generally used with WHERE and HAVING clause. + Ifyou want to select rows that must satisfy all the given conditions, you can use the logical operator - AND. + Itcan be used in any valid SQL statement such as select, insert, or delete. Example: Display name, surname and balance of all persons whose city=Ahmadabad and balance is less than 5000 SQL> SELECT name,city, balance FROM person 2 WHERE city='ahmedabad’ AND balance < 5000; Input command —> = OR operator + Logical OR compares between two Booleans as expression and returns True when ‘one of the expression is true. False otherwise. ‘© OR operator is used to combine two or more conditions. It is generally used with WHERE and HAVING dlause. If you want to select rows that satisfy one of the given condi ms, you can use the logical operator, OR Example: Display name, surname and balance of all persons whose city=Ahmadabad or balance is less than 5000. SELECT name,city, balance FROM person 2 WHERE city= ahmedabad” OR balance < S@90; ahmedabad 200 ahmedabad sse00 Snagar 1200 vatawsase Management system (333/03) ~ NOT operator + Not takes a single Boolean as an argument and changes its value from false to true or from true to false. ‘+ NOT results in the reverse of a condition. That is, if a condition is satisfied, then the row is not returned. Example: Display records of all the persons who do not belong to the city Ahmadabad. Input command —PQL> SELECT * FROM person 2 SHERE NOT citys" ahmedabad’ ; iain SR NAME SURMIME —BOATE. CITY SPI BALANCE 1 bhagya —parikh —15-JAN-10 snagar 9.315000 2vihan sagan 22-FEB-12 rangpur 7.1 10888 3 shreeje —parikh —22-JUL-10 rajkot 95060 5 tushar parnar —13-NO-98 palanpur 6.3 1988 6 aarush —parikh —15-JAN-15.snagar 812 22000 perth —parikh —15-St?-95 rajkot 9.3 $000 38 aaray —solanki 11-SEP-10 snagar 8.3 1288 rons selected, + Range searching operator (BETWEEN) ‘+ BETWEEN operator is used to check between two values or specific range. Syntax: -+ColumnName BETWEEN lower limit AND upper Selects the rows that contain values within a specified lower and upper limit. ‘+ The lower and upper limit values must be linked with the keyword AND. + The lower and upper limits are inclusive in range. Example: Display persons having balance between 10000 and 20000. Input command —»| Input command —>) wieRE city in (‘ahnedabad', ‘rangpur',‘palanpur'); vatawsase Management system (333/03) SELECT * FROM person WHERE balance BETWEEN 18006 AND 20800; Output —*) sR Nane SURNAME BATE. CITY SPI BALANCE 1 bhagya —parikh —15-AN-10 snagar 9.3 15908 2 vinan Sagar, 22-FE-12 rangpur 7.1 10000 5 tushar —parmar—_13-NOV-9@ palanpur 6.3 19900 + Set searching operator (IN) © The IN operator can be used to select rows that match one of the values in a list. Syntax: ‘© Select rows that contain any value given in a set. ‘+ This is similar to '=!, But, =! compares single value to another single value, while IN compares single value to a list (set) of values provided with IN predicate. + Itcan be used when ther anced to use multiple OR conditions. ‘+ Itcan be used with numerical, Character as well as date data type. Example: Display persons who belong to city ‘ahmedabad’, ‘rangpur’ or ‘palanpur’. SELECT * FROM person Output ——>) SR NAME SURNANE —BDATE CITY SPT. BALANCE 2vihan —sagar——(22-FEB~12 rangpur 7.2 10009 4 sanay —saxena_—-10-AUG-10 ahnedabad 8.3 11000 5 tushar —parmar—_13-NOV-98 palanpur 6.3 19808 Tavani —solanki—_15-NAY-10 ahnedabad 13 200 @misha — sutardya 22-DEC-12 ahnedabad 5.5 8000 + Character operator (LIKE) + The LIKE operator is used for character operator. © Character operators can be used to manipulate the way character strings are represented - both in the output of data as well as in providing conditions. Syntax: «--ColumnName LIKE patterns a vatawsase Management system (333/03) This is similar to '=', BUT, the '=' operator compares for exact matching, While LIKE compares for pattern similarity. ‘+ The LIKE operator is used with two special characters > % and _ (underscore). = % (modulo) allows matching with any string having any number of characters. + _ (underscore) allows matching with single character, Examples of LIKE operator are explained below: 1. Display name of persons starting with ‘a’. Input command —> output. —> - a jihan Sagar want Solanki she sutardya faravsollank, + Concatenation operator ( | | ) ‘¢ It is used to combine two strings, and specially used for displaying output. + Strings should be constant as well as column names having character data types. ss PR vatasase management system (5550/05) Example: Display person full name (combing name and surname) from ‘person’ table. Input command —» SQL> SELECT name [| surname "FULL NAMI 2. FROM person; ‘Output. —> ‘Write SQL queries to perform following tasks: (Consider the tables created in practical no. 3) 1. WAQ (Write a Query) to display all persons who are have balance greater than 20000 from person table. nagar and 2. WAQ to find faculty name and subject who are taking (teaching) subject either DBMS or OS from faculty table. 3. WAQ to list all the employees who are not clerk from employee table. 4, WAQ to find the id and name of the faculty who are in computer department and not taking OS subject from faculty table. DataBase Management System (3330703) 5. WAQ to display name and age of all the records having age between 5 to 15 from test table. 6. WAQ to display all the persons who belong to city ‘rajkot’, ‘surendranagar’, ‘palanpur’. 7. WAQ to find the records that have ‘a in faurth character of name from test table. 8. WAQ to display name, surname of all the persons whose surname has length of 6 characters from person table. 9. WAQ to display name, surname and city of all the persons whose surname start with ‘p’ and ends with ‘h’ from person table, 10. WAQ to dicplay name and birth date of all persons who born after 31 December 2010 from person table. 11. WAQ to display full name of all the persons (Full name should be name surname, from person table. Aa SaaS ! | | {Signature of Faculty.

You might also like