当前位置:网站首页>Database interview questions +sql statement analysis

Database interview questions +sql statement analysis

2022-06-27 00:53:00 ˡ A cup of American style

Interview questions 1 ( Table creation data is also available )

The following is the user order form of a delivery company 、 Merchant DB surface 、 Please write down the question sql sentence .

1、1 Each monthly consumption is greater than 20 The total consumption amount of RMB users

2、1 The number of people who only ate Malatang and hamburger in June

3、 Count everyone bd_name Of BD Sales of the corresponding stores

create table t_user(
	uid varchar(10) not null comment ' user ID',
	order_time timestamp null comment ' Order time ',
	order_category varchar(20) not null comment ' type ',
	order_amt float not null default '0.00' comment ' Price ',
	shop_id varchar(10) not null comment ' shops ID'
)comment ' User information sheet ';

create table t_shop(
	shop_id varchar(10) not null comment ' shops ID',
	bd_name varchar(10) not null comment ' sales manager ',
	bd_team varchar(10) not null comment ' The sales team ',
	start_time varchar(10) not null comment ' Starting time ',
	end_time varchar(10) not null comment ' End time '
)comment ' Shop information sheet ';

insert into t_shop(shop_id,bd_name,bd_team,start_time,end_time) values
('ZL123',' Xiao Ming ',' sales A Group ','2018-01-01','2018-01-14'),
('ZL123',' Xiao Zhang ',' sales B Group ','2018-01-15','2099-12-31'),
('SM456',' Xiao Zhang ',' sales B Group ','2016-01-01','2019-01-14'),
('HBW123',' petty thief ',' sales C Group ','2015-01-01','2020-12-31'),
('XM456',' petty thief ',' sales C Group ','2015-01-01','2016-01-14');

insert into t_user(uid,order_time,order_category,order_amt,shop_id) values
('A123','2018-01-01 12:34:00',' Spicy Hot Pot ',25.30,'ZL123'),
('A123','2018-01-06 12:34:00',' porridge ',34.20,'SM456'),
('B456','2018-01-15 12:34:00',' Spicy Hot Pot ',25.30,'ZL123'),
('B456','2018-01-25 12:34:00',' Hamburger ',36.30,'HBW123'),
('C789','2018-02-01 12:34:00',' Crayfish ',19.80,'XM456');

select * from t_user;
select * from t_shop; 

#1 Each monthly consumption is greater than 20 The total consumption amount of RMB users 
# Conditions :1 month + Greater than 20+
select month(now()) from daul;
select round(sum(order_amt),2) from t_user where month(order_time)=1
and order_amt>20;

#1 The number of people who only ate Malatang and hamburger in June 
# Conditions :1 month +(' Spicy Hot Pot ' and ' Hamburger ')
select count(uid) from t_user where month(order_time)=1 and order_category in (' Spicy Hot Pot ',' Hamburger ');

# Count everyone bd_name Of BD Sales of the corresponding stores 
select s.shop_id,round(sum(u.order_amt),2) from t_user u,t_shop s where
u.shop_id=s.shop_id GROUP BY s.shop_id;
select shop_id,round(sum(order_amt),2)from t_user GROUP BY shop_id;


Interview questions 2

1. Write the table Department Add a record and Update a record SQL sentence
Increase the record value (‘12’, ‘ R & D department ’, ‘ Zhang San ’) ; 
to update dept_id=’12’ The record of (‘12’, ‘ R & D department ’, ‘ Zhangsanxin ’) ;

2. Need to give a watch Department Add a column of fields notes, The length is 10 String , The default value is ‘0’ , Please write about SQL sentence

3. Find salary greater than 2000 Employee records of yuan , And according to the employee number id Ascending order

4. Find salary greater than 2000 Yuan's employee's Department 、 Department number 、 division manager 、 Employee name

5. Find the names of all the staff in the Department where Zhang San and Li Si work

6、 View the Department Manager and department number of each department , Sort by department number ?

7、 Delete table Department All records in

8、 Delete table Department

Create Table Department(
dept_id varchar(2) not null comment ' Department number ',
dept_name varchar(20) not null comment ' Department name ',
dept_leader varchar(10) comment ' division manager '  
)comment ' Departmental table ';

Create Table Personnel(
id varchar(4) not null comment ' Employee number ',       
name varchar(10) not null comment ' full name ',      
dept_id varchar(2) not null comment ' Department number ',   
age integer comment ' Age ',                   
gzsj date comment ' Working hours ',                     
technical_post varchar(10) comment ' The title ',   
salary integer comment ' salary '                
)comment ' The employee table ';

select * from Department;
select * from Personnel;

#1 Write the table Department Add a record   and   Update a record  SQL sentence 
# Increase the record value  ('12', ' R & D department ', ' Zhang San ') ; 
# to update  dept_id='12' The record of  ('12', ' R & D department ', ' Zhangsanxin ') ;
insert into Department(dept_id,dept_name,dept_leader) values('12',' R & D department ',' Zhang San ');
insert into Department(dept_id,dept_name,dept_leader) values('13',' R & D department ',' Li Si ');
insert into Department(dept_id,dept_name,dept_leader) values('14',' R & D department ',' Wang Wu ');
insert into Department(dept_id,dept_name,dept_leader) values('15',' R & D department ',' Zhao Liu ');
update Department set dept_leader=' Zhangsanfa ' where dept_leader=' Zhang San ';

#2 Need to give a watch Department Add a column of fields notes, The length is 10 String , The default value is ‘0’ ,  Please write about SQL sentence 
alter table Department add notes varchar(10) default 0;

#3 Find salary greater than 2000 Employee records of yuan , And according to the employee number id Ascending order 
insert into Personnel(id,name,dept_id,age,technical_post,salary) values
('1',' Xiao Ming ','12',23,' Technical director ',12000),
('2',' Xiao Zhang ','13',18,' project manager ',10500),
('3',' Xiao Hu ','14',20,' The product manager ',20000),
('4',' petty thief ','15',21,' CEO ',30000);
select * from Personnel where salary>2000 order by id;

#4 Find salary greater than 2000 Yuan's employee's Department 、 Department number 、 division manager 、 Employee name 
select d.dept_name,d.dept_id, d.dept_leader,p.name from Personnel p inner join Department d on p.dept_id=d.dept_id where p.salary>2000;

#5 Find the names of all the staff in the Department where Zhang San and Li Si work 
select name from Personnel where name =' Zhang San ' and name=' Li Si ';
select name from Personnel where dept_id in(select dept_id from Personnel where name =' Zhang San ' and name=' Li Si ');

#6 View the Department Manager and department number of each department , Sort by department number ?
select d.dept_leader,count(p.id) from Department d left outer join Personnel p on p.dept_id=d.dept_id group by d.dept_leader order by count(p.id);

#7 Delete table Department All records in 
delete from Department;

#8 Delete table Department
drop table Department;

Interview questions 3

utilize Oracle Of case function , To use an sql The query results are as follows

1 Show the number of boys in each department 、 Number of girls and total number

2 Show the number of boys in each department 、 Number of girls and total number , And the number of female students in this department >=1, And sort by department label in descending order

drop table kingstar;
select * from kingstar;
create table kingstar(
	dept_no char(4),
	person_no int,
	sex char(1),
	salary decimal(19,4)
);
insert into kingstar(dept_no,person_no,sex,salary) values('H001',1210,'M',1234.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H001',1211,'f',900.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H002',1212,'f',3000.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H002',1213,'M',4500.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H003',1214,'M',6394.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H003',1215,'f',7900.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H004',1216,'M',2300.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H004',1217,'M',3400.00);
insert into kingstar(dept_no,person_no,sex,salary) values('H005',1218,'M',3200.00);

#1 Show the number of boys in each department 、 Number of girls and total number 
select dept_no,
sum(case when sex = 'M' then 1 else 0 end) Number of boys ,
sum(case  when sex = 'f' then 1 else 0 end) Number of girls ,
count(*) The total number of 
from kingstar GROUP BY dept_no;

#2 Show the number of boys in each department 、 Number of girls and total number , And the number of female students in this department >=1, And sort by department label in descending order 
select dept_no,
sum(case when sex = 'M' then 1 else 0 end) Number of boys ,
sum(case when sex = 'f' then 1 else 0 end) Number of girls ,
count(*) The total number of 
from kingstar GROUP BY dept_no having sum(case when sex = 'f' then 1 else 0 end)>=1 ORDER BY dept_no desc;

Interview questions 4

Use scott/tiger Under the user emp Table and dept Please complete the following exercises .

1 List all employees whose salary is higher than the average salary of the company

2 List salary higher than in Department 30 The name and salary of all employees working

3 List the number of employees working in each department , Average wage and average service life
# Find out avgTime, Look at the data in the table , original sum(avg_time) Just simply add the result after removing the special characters from the string .

4 List details of all departments and number of departments

5 List the minimum wage for all kinds of jobs

6 List the departments MANAGER( The manager ) The minimum wage for


#1 List all employees whose salary is higher than the average salary of the company 
select * from emp where sal in(select(sal) from emp);

#2 List salary higher than in Department 30 The name and salary of all employees working 
select e.ename,e.sal,d.dname from emp e join dept d on e.deptno = d.deptno where e.sal > (select max(sal) from emp where deptno=30);

#3 List the number of employees working in each department , Average wage and average service life 
# Find out avgTime, Look at the data in the table , original sum(avg_time) Just simply add the result after removing the special characters from the string .
select d.deptno, count(e.ename) as total_emp, ifnull(avg(sal), 0) as avgsal,ifnull(avg((TO_DAYS(NOW())-TO_DAYS(e.hiredate))/365),0) as avgTime from emp e right join dept d on e.deptno = d.deptno GROUP BY d.deptno;

#4 List details of all departments and number of departments 
select d.*, count(e.ename) from emp e right join dept d on e.deptno = d.deptno GROUP BY d.deptno,d.dname,d.loc;

#5 List the minimum wage for all kinds of jobs 
select job,min(sal)  minimum wage  from emp GROUP BY job;
 
#6 List the departments MANAGER( The manager ) The minimum wage for 
select deptno,min(sal) from emp where job='MANAGER' GROUP BY deptno;

This is today's sharing !

原网站

版权声明
本文为[ˡ A cup of American style]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270008525646.html