Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.

Similar presentations


Presentation on theme: "Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values."— Presentation transcript:

1 Comp12 cont…

2 Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values (most database systems will also accept double quotes). Numeric values should not be enclosed in quotes.

3 For text values: This is correct: SELECT * FROM Persons WHERE FirstName = 'Tove' This is wrong: SELECT * FROM Persons WHERE FirstName = Tove

4 For numeric values: This is correct: SELECT * FROM Persons WHERE Year > 1965 This is wrong: SELECT * FROM Persons WHERE Year > '1965'

5 IN operator: IN operator in the where clause can be used to check for discrete values.

6 Example 1: Write a query to get those names of all students whose marks are 90 or 92. Ans: SELECT name FROM student WHERE marks IN (90, 92);

7 Example 2: Write a query to get details of all students whose belong to class C12 or E12. Ans: SELECT * FROM student WHERE class IN (‘C12’, ‘E12’);

8 BETWEEN-AND Operator: This operator is used to define a range of values. The lower and higher value is also included in the range of values.

9 Example: Write a query to display the roll & name of all students whose marks is between 90 & 95. Ans: SELECT roll, name FROM student WHERE marks BETWEEN 90 AND 95;

10 LIKE operator: This operator is used to check character type fields. It is used to check substrings. There are two types of wild cards used with LIKE operator. Underscore (_): It stands for a single character. e.g.: h_t can stand for hat, hit, hot etc. Percentage (%): It stands for occurrence of 0 or more characters.

11 Example 1: Select name, roll from student where name like ‘Sh%’;

12 Example 2: Write a query to display the details of all students whose names start with ‘R’; Ans: SELECT * FROM student WHERE name LIKE ‘R%’;

13 LOGICAL or BOOLEAN operators: The logical operators AND, OR, NOT are used to have two or more criteria in a single command.

14 Example 1: Write a query to display the name and roll of all students who are in class C12 and their age is 15. Ans: SELECT name, roll FROM student WHERE class = ’C12’ AND age = 5;

15 Example 2: Write a query to display the details of all students who are in class E12 or whose age is above 13. Ans: SELECT * FROM student WHERE class = ‘E12’ OR age >13;

16 Aggregation function: AVG - Get the average of a specified column. COUNT - Get the quantity of values in the column. MAX - Return the maximum value in a specified column. MIN - Return the minimum value in a specified column. SUM - Return the sum of all numeric values in the specified column.

17 Example: SELECT MAX(population)FROM citylist;WHERE state = 'Indiana';

18 1. Write the correct SQL commands for the following on the basis of table Supplier.

19 No.NamePriceSupplierStock 1Motherboard7000Intel20 2Keyboard1000TVSE70 3Mouse500Logitech60 4Soundcard600Samsung50 5Speaker600Samsung25 6Monitor3000Philips22 7CD-ROM2800Creative32 8Printer7900HP10

20 1.List all Name and Price with price between 3000 and 7000. 2.To set the price to 1200 for ‘Keyboard’. 3.To delete rows with stock between 20 and 40. 4.To display Name, Price, stock of all suppliers whose name starts with 'S'. 5.To display supplier without duplication. 6.To insert a row with appropriate values. 7.To increase the stock of all Samsung suppliers. 8.To display all supplier details whose supplier is Philips and stock is above 40. 9.To display name, supplier, stock for name as keyboard or mouse.

21 1.SELECT Name, Price FROM Supplier WHERE Price BETWEEN 3000 AND 7000; 2.UPDATE Supplier SET Price=Price + 1200 WHERE Name = ‘Keyboard’; 3.DELETE FROM Supplier WHERE Stock BETWEEN 20 AND 40; 4.SELECT Name, Price, Stock FROM Supplier WHERE Supplier LIKE ‘S%’; 5.SELECT DISTINT Supplier FROM Supplier; 6.INSERT INTO Supplier VALUES(9,’Headphone’, 50, ‘Logitech’, 10); 7.UPDATE Supplier SET Stock=Stock + 10; 8.SELECT * FROM Supplier WHERE Supplier=’Philips’ AND Stock > 40; 9.SELECT Name, Supplier, Stock FROM Supplier WHERE IN(‘Keyboard’,’Mouse’);


Download ppt "Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values."

Similar presentations


Ads by Google