SQL statement for Java Pen questions (single table)

Source: Internet
Author: User

Title: There is a table in the system Wcemploy (employee number, name, department name, job, salary)

I. Please write out the statement of the building form

1 CREATE TABLE Wcemploy (2     int auto_increment PRIMARY KEY, 3     Char (8null,4     Char (+), 5     Char (8), 6     Double 7 )

Two. Inserting data

1 Insert  intoWcemployValues(NULL,'Zhang San','Workshop One','Fitter',6000)2 Insert  intoWcemployValues(NULL,'John Doe','Workshop One','Electrician',8000)3 Insert  intoWcemployValues(NULL,'Harry','Workshop II','Workshop Director',10000)
1 Insert  intoWcemployValues(NULL,'Tai Day Day','Workshop One','Fitter',6000),2(NULL,'Wind High set fire','Workshop One','Electrician',8000),3(NULL,'Volvo','Workshop II','Workshop Director',10000),4(NULL,'CVBS','Workshop II','Fitter', -),5(NULL,'Single person','Workshop Three','Fitter',4500),6(NULL,'Head Dog','Workshop II','Fitter',6000),7(NULL,'Harem','Workshop One','Fitter', the)

Multiple data insertions

When inserting the data, an error occurred, inserting the Chinese character data errors

Modify the database properties of the MySQL database property character set to

Three. Query statements

1. Please use an SQL statement to query the total number of people in each department

Select department_name,COUNT(DISTINCT from wcemployGROUP  by Department_name

2. Use an SQL statement to find out the average salary of the staff in different departments as "fitter"

Select department_name,AVG from wcemploywhere type= ' Fitter ' GROUP  by department_name//If there is no grouping, an error occurs

3. Please use an SQL statement to find out the department of the staff with the "fitter" average salary higher than 2000 in different departments

Select department_name,AVG(Salary) as from wcemploywhere type = ' Fitter ' GROUP  by Department_name  having AVG (Salary) >  -

Error wording:

Select department_name,AVG(Salary) as from wcemploywhere type = ' Fitter '   and AVG (Salary) >  - GROUP  by Department_name

4. Please use an SQL statement to query employee information for each department below average salary

Select *  from Wcemploy W, (SelectAVG as davg,department_name//a w table  from WcemployGROUP by department_name) t//a T-table where=  and W.salary<DAvg

1 SelectW.*, DAvg from2 Wcemploy W3  Left Join(Select AVG(Salary) asDavg,department_name4  fromWcemploy5 GROUP  byDepartment_name) T onW.department_name=T.department_name6 whereW.department_name=T.department_name andW.salary<DAvg

  

SQL statement for Java Pen questions (single table)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.