Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.7 kB
13
Indexable
Never
01/03/2024
==========================================================================
distinct keywords:
- the distinct is used for return only unique(diffrerent) value.
syntax: select distinct column from tablename
ex:
select name from employeeinfo;
select  distinct name from employeeinfo;
select  distinct name,salary from employeeinfo;
===================================================================
Aggregate function:
1.count()
2.min()
3.max()
4.avg()
5.sum()

use testdb;
select * from customer;

select count(*) from customer;
select count(address) from customer;
select count(distinct address) from customer;
select count(*) from customer where age>=35;

select min(cust_id) from customer;
select min(cust_name) from customer;
select min(cust_name) from customer where age>=35;

select max(cust_id) from customer;
select max(cust_name) from customer;
select max(cust_name) from customer where age>=35;

select avg(age) from customer;
select avg(age) from customer where address="pune";

================================================================================================
Group by clause:-
-------------------
group by:
group by clause is used for grouping data.
the group by clause groups rows that have same value.
the group by clause must used with aggregate function(count(),min(),max(),avg(),sum())

syntax:
select column() from tablename group by column;

use testdb;
select * from customer;

select * from customer group by age;
select * from employee;
select dept ,count(*) from employee group by dept;
select dept, max(salary) from employee group by dept;
select dept, min(salary) from employee group by dept;

select dept, min(salary) from employee group by dept order by dept Desc;
select dept, min(salary) from employee where city="pune" group by dept order by dept Desc;

====================================================================================================
when we use where and group by statement in single query
then the sequence is 
1.where
2.group by

select count(*),address from employeeinfo where salary>20000 group by address;
-------------------------------------------------------------------------------------------------------
when we use group by clause,order by clause and where clause at a one time then the squence is
1 where 
2 group by
3.order by

select count(*),address from employeeinfo where salary>20000   group by address order by address ;

================================================================================================================================
Limit clause:
limit clause is used for return specific number of records.

syntax:-select columns from tablename limit number;
select * from customer limit 6;
select cust_name,city from customer limit 3;

select * from employeeinfo where salary=30000;
select * from employeeinfo where salary=30000 limit 2;
select * from employeeinfo where salary=30000 order by name ,id limit 2;

use testdb;
select * from customer;

select * from customer limit 10;
select * from customer  order by cust_id desc limit 5;

syntax:- select columns from tablename limit limit_values offset offset_value;
select * from customer limit 3 offset 5;

syntax:-select columns from tablename limit offset_value, limit_value
select * from customer limit 5,3;
select * from customer  order by cust_id desc limit 5 offset 5;


=====================================================================================================================================

assignment:

Q1.first create following table and insert value,

Q2.From the orders table, write a SQL query to calculate the average purchase amount of all orders.
Return average purchase amount. 

Q3.From the following table, write a SQL query that counts the number of unique salespeople. Return number of salespeople.  

Sample table: orders

ord_no      purch_amt   ord_date    customer_id  salesman_id
----------  ----------  ----------  -----------  -----------
70001       150.5       2012-10-05  3005         5002
70009       270.65      2012-09-10  3001         5005
70002       65.26       2012-10-05  3002         5001
70004       110.5       2012-08-17  3009         5003
70007       948.5       2012-09-10  3005         5002
70005       2400.6      2012-07-27  3007         5001
70008       5760        2012-09-10  3002         5001
70010       1983.43     2012-10-10  3004         5006
70003       2480.4      2012-10-10  3009         5003
70012       250.45      2012-06-27  3008         5002
70011       75.29       2012-08-17  3003         5007
70013       3045.6      2012-04-25  3002         5001

Leave a Comment