select ,update command in sql.pptx
select ,update command in sql.pptx
Informatics Practices
Class XI
Select : This command can be used to execute a query
and to retrieve information from one or more tables.
Format/Syntax:
select fieldnames
from < table name >
[where <condition>
order by <fieldname> [desc]
group by <fieldname>
having <condition> ];
Options with Select command
1. Use of *
2. Use of Distinct
3. Use of simple calculations
4. Use of where
5. Use of relational/logical operators
6. Use of in/between
7. Use of NULL
8. Use of wild card characters ( % and _ ) and LIKE keyword
9. Use of order by (asc-default / desc)
Options with Select command
1. Use of *
→Use of * with Select statement picks up all fields automatically for display.
2. Use of Distinct
→ Distinct Keyword:
By default, SQL shows all the data retrieved through query as output. However,
there can be duplicate values in a field. The SELECT statement when combined
with DISTINCT clause, returns values without repetition (distinct values) from a
particular field.
Syntax: Select distinct <fieldname> from<tablename>;
Note: Do not use any other fieldname or condition when using distinct
keyword.
Options with Select command
3. Simple calculations
→Using select we can do calculations too which may involve fields or
may not
Ex: Select 8*9+10;
Select name, sal, 0.50* sal, 0.30 *sal from emp
4. Use of where clause
→Where clause of select command is used to filter records from the
table. By filtering I mean, Selecting records based on a specified
condition.
Ex: Select * from emp where salary>45000;
Select * from emp where DOJ >”2004-01-30” and dept=“sales”;
SC INAME COST QTY DOPUR WARN Questions:
1 Carom 4000 20 27-Mar-98 3 1. Create the table.
2 Bat 10000 10 17-May-99 4 2. Insert the records
3 TTennis 8300 20 15-Apr-03 null 3. WAQ to display all records.
4 Pad 2150 10 23-Nov-91 4 4. WAQ to display item names along with cost for all items.
5. WAQ to Date of purchase and itemname.
5 Wicket 2500 10 07-Jun-93 3
6. WAQ to display the unique item names from the table.
6 Chess 2400 20 09-Aug-02 4 7. WAQ to display the name of items without repetition.
7 LTennis 25250 12 27-Mar-98 2 Use of Conditions:
8 Football 5450 20 07-Oct-92 3 8. WAQ to display the itemnames whose cost is more than 3000.
9 Hockey 5000 null 29-Mar-94 2 9. WAQ to display the code and itemnames which has been
10 Cork 300 10 01-Nov-01 null purchased after 1995 and cost <5000.
10. WAQ to display a report specifying itemname , cost and discount
Solutions: as 15% of price .Use column aliasing too.
3. Select * from item;
4. Select iname, cost from item; 10. Select iname, cost, 0.15*price AS “ Discount” from item ;
5. SELECT DOPUR, INAME FROM ITEM;
6, 7. Select distinct iname from item;
8. Select Iname from item where cost>3000;
9. Select sc,iname from item where DOPUR >’1995-12-31’ and cost <5000;
sc iname
1 Carom
2 Chess
3 Cork
Use of Membership Operator IN with Select Command
IN keyword with Select Command is used to specify or check for multiple values in a field.
The IN operator compares a value with a set of values and returns true if the value belongs to that set. It is
used in place of using the Logical OR operator.
Ex:
Select * from emp where dept IN(“admin”, ”sales”, ”Accounts”);
Advantage of using IN keyword in the above example lets us not to use the field name multiple times.
Use of Between keyword with Select Command
Between keyword with Select Command is used to specify a range of values in a field. It is used in place of
using the Logical AND operator
Ex:
Select * from emp where salary BETWEEN 5000 AND 15000;
Select * from emp where salary >= 5000 and salary <=15000;
Advantage of using BETWEEN keyword in the above example lets us not to use the field name multiple times.
Use of LIKE with Select Command
LIKE keyword is used to refer to a group of records (pattern matching) . We use 2 wild card characters to
create the group references:
Wild Card Characters are:
a) _ (underscore) : which refers to compulsory presence of a single character
b) % : which refers to presence of zero or more characters.
Example:
a) Names which starts with S and ends with A. S%A
b) Names which have T as the third character. _ _ T%
c) Names which end with Kapoor % Kapoor
d) Names which start with Anil. Anil%