6.SQL Queries Clause
6.SQL Queries Clause
CLAUSES
1.WHERE CLAUSE
• The WHERE clause is used to extract only those
records that fulfill a specified criterion.
• SELECT column_name,column_name
FROM table_name WHERE column_name operator
value;
• Select * from student where id=3;
• Select namr from student where id=3;
• WHERE clause uses some conditional selection.
• insert into student where = equal
> greater than
(cgpa=5 and cgpa<=8); < less than
Maximum salary in
EMPLOYEES table
…
Types of Group Functions
– AVG
– COUNT
– MAX
– MIN
– STDDEV
– SUM
– VARIANCE Group
functions
• SQL GROUP BY Syntax
• SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator
value GROUP BY column_name;
SQL AGGREGATE FUNCTIONS
6. UPPER()
• UCASE function is used to convert value of string
column to Uppercase character.
• Syntax of UCASE,
• SELECT UPPER(column_name) from table-name;
• SQL query for using UPPER is,
• SELECT UPPER(name) from emp;
7.LOWER()
• LCASE function is used to convert value of string
column to Lowecase character.
• Syntax for LCASE is,
• SELECT LCASE(column_name) from table-name;
• SQL query for converting string value to Lower case
is,
• SELECT LCASE(name) from emp;
8. SUBSTR()
• MID function is used to extract substrings from
column values of string type in a table.
• Syntax for MID function is,
• SELECT substr(column_name, start, length) from
table-name;
• SQL query will be,
• select substr(name,2,2) from emp;
9.ROUND()
• ROUND function is used to round a numeric field to
number of nearest integer. It is used on Decimal
point values. Syntax of Round function is,
• SELECT ROUND(column_name, decimals) from
table-name;
• SQL query is,
• SELECT ROUND(salary) from emp;
Group By Clause
• Group by clause is used to group the results of a SELECT
query based on one or more columns. It is also used with SQL
functions to group the result from one or more tables.
• Syntax for using Group by in a statement.
• SELECT column_name, function(column_name) FROM
table_name WHERE condition GROUP BY column_name;
• Here we want to find name and age of employees grouped by
their salaries
• SQL query for the above requirement will be,
• SELECT name, age ,max(salary)from Emp group by
name ,age;
HAVING CLAUSE
SUBSTITUTION VARIABLES
• 1.used to temporarily store values with & and &&.
• 2.used with where ,order by ,select statements.
• we can use predefined variables by using define
command. define creates and assigns a value to a
variable.
• when sql developer detects that sql statement contains
an ampersand,you are prompted to enter a value for
the substitution variable that is named in sql
statement.
• after you enter a value and click ok,results are
displayed.
• after you enter a value and click ok,results are
displayed.e.g.
• 1. select * from emp where eid=&employeenum;
• enter here 401 or 402 or 403 as employeenum
• 2.select salary+12 from emp where eid='&enumb';
• 3.select eid , lastname, jobid, & columnname from
emp where &condition order by & ordercolumn
• in 3rd point,columnname can be salary,condition can
be salary>1500, ordercolumn can be lastn:
• Eg:select eid,name, &age from emp where
salary>5000 order by &name;
• ********Using && substitution variable*******
• use && if u want to reuse the variable value
without prompting use each time.
• select eid,lastname,jobid,&&columnname from
emp0 order by &columnname;
• enter salary as columnname
• no need to enter it again,every time you run the
above command same column name will be picked.
DEFINE AND VERIFY
• 1. Define-This command is used to create and assign a value to
a variable.
• 2. Undefine-To remove a variable.
• define employeenum=401;
• select * from emp where eid=&employeenum;
• insert into emp values(&eid,&name,&age,&salary);
• undefine employeenum;
• here defined variable value is automatically substituted in
select statement.
• employeenum variable is present in session until the user
undefines it or exits the sql developer session.
• Verify-to confirm the changes in sql statement
• It is used to toggle the display of substitution
variable
• set verify on
• select eid,lastname,salary,jobid from emp0 where
eid=&employeenum;
• Already present in 12c