Write a SQL query to get the second highest salary (Salary) in the Employee table

Single choice

class X{
    
    
	Y y=new Y();
	public X(){
    
    
		System.out.print("X");
	}
}
class Y{
    
    
	public Y(){
    
    
		System.out.print("Y");
	}
}
public class Z extends X{
    
    
	Y y=new Y();
	public Z(){
    
    
		System.out.print("Z");
	}
	public static void main(String[] args) {
    
    
		new Z();
	}
}

A.ZYXX

B.ZYXY

C. YXYZ

D.XYZX

public interface IService {String NAME=“default”;}

Which item does the default type equivalent represent:

  • A.public String NAME=“default”;
  • B.public static String NAME=“default”;
  • C .public static final String NAME=“default”;
  • D.private String NAME=“default”;
String s1="abc"+"def";//1
String s2=new String(s1);//2
if(s1.equals(s2))//3
	System.out.println(".equals succeeded");//4
if(s1==s2)//5
	System.out.println("==succeeded");//6

A. Lines 4 and 6 are not executed.

B. Line 6 is executed, but line 4 is not executed.

C. Line 4 is executed, but line 6 is not executed.

D. Lines 4 and 6 will both be executed.

public class TestClass {
    
    

  private static void testMethod(){
    
    
    System.out.println("testMethod");
  }

  public static void main(String[] args) {
    
    
	((TestClass)null).testMethod();
  }
}

A. Compilation fails

B. Compilation passed, running exception, NullPointerException reported

C. Compilation passed, running exception, reporting IllegalArgumentException

D. Compilation passed, running exception, NoSuchMethodException reported

E. The compilation is passed, the operation is abnormal, and an Exception is reported.

F. Runs normally and outputs testMethod

Which of the following descriptions of JAVA exception classes is wrong?

  • A. Exception inheritance structure: the base class is Throwable, Error and Exception inherit Throwable, RuntimeException and IOException inherit Exception
  • B. Non-RuntimeException is generally an external error (non-Error), which must be captured by the try{}catch statement block
  • C. The Error class system describes internal errors and resource exhaustion situations in the Java operating system. Error does not need to be captured.
  • D. The RuntimeException system includes incorrect type conversion, array out-of-bounds access, attempts to access null pointers, etc., which must be caught by the try{}catch statement block

.Start a complete binary tree with 100 nodes from the root level, and number the nodes in each level from left to right. The root node number is 1, then the parent node number of the node numbered 98 is

  • A.47
  • B. 48
  • C.49
  • D.50

How many levels can a complete binary tree with n nodes have at most?

  • A.n/2
  • B. log(n)+1(rounded down)
  • C.n-1
  • D.n

What does not belong to a relational database is ( )

  • A.Oracle
  • B. Essbase
  • C.SQL Server
  • D.DB2

Regarding relational database, the correct and comprehensive description is: ( )

  • A. In the relational model, the logical structure of data is a two-dimensional table.
  • B.DML is a language between relational algebra and relational calculus. It fully embodies the characteristics and advantages of relational database language.
  • C. The integrity rules of the relational model are some constraints on the relationship, which are divided into entity integrity and referential integrity constraints.
  • D. In a relational database, relationships are also called databases, tuples are also called rows, and attributes are also called columns.

Option B should be "SQL is a language between relational algebra and relational calculus, which fully embodies the characteristics and advantages of relational database languages". SQL includes DDL, DCL and DML; option C misses the integrity of user definitions. , should be "The integrity rules of the relational model are certain constraints on the relationship, which are divided into entity integrity, referential integrity constraints and user-defined integrity"; in option D, the relationship is a table, not a database. It should be "In a relational database, relationships are also called tables, tuples are also called rows, and attributes are also called columns"; so choose A.

Each record in a relational database file ( ).

  • A. The order of front and back cannot be reversed arbitrarily, and must be arranged in the order of input.
  • B. The order can be reversed arbitrarily without affecting the data relationship in the library.
  • C. The order before and after can be reversed at will, but if the order is different, the results of statistical processing may be different.
  • D. The order of front and back cannot be reversed arbitrarily, and must be arranged in the order of keyword field values.

The purpose of relational database normalization is to solve ()

  • A. Insertion, deletion exceptions and data redundancy issues
  • B. The problem of low query speed
  • C. Problems with complex data operations
  • D. Issues of data security and integrity assurance

In option B, query is just a way to display data, and the core of the database is definitely not here. As for option C, this should be more concerned about the data structure, not the database itself. As for option D, sorry file system means you are just a younger brother. So choose A. The database itself manages data, and it must focus on the data itself, naturally to solve problems related to the data itself!

In SQL, which of the following operations involving null values ​​is incorrect?

  • A.name IS NULL
  • B. name= NULL
  • C.namee IS NoT NULL
  • D.NoT(name IS NULL)

In SQL, NULL is a unique data type, which is equivalent to having no value and being an unknown number. NULL is different from 0, empty string, and space. By default, SQL will always return 0 rows when judging WHERE XX!=NULL and WHERE XX=NULL, but will not prompt a syntax error.

Subject to satisfying entity integrity constraints

  • A. There should be one or more candidate keywords in a relationship
  • B. There can only be one candidate keyword in a relationship
  • C. There must be multiple keys and multiple candidates in a relationship
  • D. There can be no candidate keywords in a relationship

Database integrity is divided into entity integrity, domain integrity and referential integrity. Entity integrity requires that the primary key fields in the table cannot be empty and cannot be repeated; domain integrity requires that the data in the table are within the valid range; referential integrity ensures the data consistency of related tables.

Constraints are a means to ensure the integrity and consistency of data in a table, and are divided into five types: primary key constraints, foreign key constraints, check constraints, unique constraints, and non-null constraints. Regardless of the constraint, there can be one or more columns in the table.

If entities A and B have a one-to-many relationship, and entities B and C have a one-to-one relationship, what is the relationship between entities A and C?

  • A. One to one
  • B. One to many
  • C. Many to one
  • D. Many-to-many

What are the errors in the difference and usage of WHERE, GROUP BY, and HAVING keywords in SQL query statements?

  • A.HAVING must rely on GROUP BY in the query statement
  • B. The WHERE clause is used to limit the SELECT statement to specify selected rows from the table.
  • C.GROUP BY clause is used to group the output result set of the WHERE clause
  • D. The HAVING clause is used to filter columns from grouped results.

HAVING filters data (rows) from grouped results, not columns.

It is known that there are 2 rows of data in table T1 and 3 rows of data in T2. ​​After executing the SQL statement, "select a.* from T1 a, T2 b", the number of rows returned is

  • A.2
  • B.3
  • C.5
  • D. 6

When the associated query does not have any conditions, the result of the Cartesian product is returned. The number of rows is the product and the number of columns is the sum of the columns of the two tables.

When Mysql queries, only records that meet the join conditions are included in the query results. This join is

  • A.Left join
  • B. Right join
  • C.Inner join
  • D.Full connection

Only inner joins will always filter according to the join conditions. Outer joins will also return results when the data in the table does not meet the join conditions.

Which of the following statements about JDBC is incorrect?

  • A. DriverManager can obtain database connection
  • B.The database connection obtained by DataSource can be reused
  • C.The database connection obtained by DataSource also needs to close the physical connection
  • D.DataSource also needs to load the database driver to obtain the database connection.

Which of the following statements is wrong?

  • A. Using JDBC has better portability
  • B.JDBC has formulated unified database programming specifications and requires the database manufacturer to provide driver packages.
  • C. JDBC can also be used to connect to NoSQL databases, such as Redis
  • D.JDBC can also execute DDL instructions

The wrong description about PreparedStatement and Statement is

  • A. Generally speaking, PreparedStatement executes more efficiently than Statement
  • B.PreparedStatement will precompile SQL statements
  • C.Statement will parse/compile SQL every time, establish and optimize the data acquisition path
  • D. The result set of Statement execution scan is larger than PreparedStatement

There is an employee table emp, with fields: name, gender, department, and salary. Query the following data:

1. Check the average salary of male and female employees

2. Check the total salary of each department

3. Query the department with the second highest total salary

4. Query employee information with duplicate names

5. Query the average salary of male employees in each department whose salary is greater than 10,000

select sex, avg(salary) from emp group by sex;


select depart, sum(salary) from emp group by depart;


select depart, sum(salary) form emp group by depart having order by (salary) desc limit 1;


select * from emp as e1, emp as e2 where s1.name = s2.name;


select depart, avg(salary) from emp where sex = 'male' and salary > 10000 group by depart;

1:select sex,avg(salary) from emp group by sex;

Note: The average value uses the aggregation function avg, and is grouped by gender by male and female, group by gender field

2:select depart,sum(salary) from emp group by depart;

Description: The total salary uses the aggregate function sum to sum the salary fields and group by department fields, group by department fields

3:select depart,sum(salary) from emp group by depart order by sum(salary) desc limit 1,1;

Note: The order by statement first sorts according to the total salary, and then takes the second piece of data. You can use paging. There is 1 piece of data on each page, and the second page is the result.

4:select name from emp group by name having count(name)>1;

Note: Duplicate names mean that there are multiple records of the same name. You can group them by name first. After grouping, filter the rows with a number greater than 1. This means that there are at least 2 records with the same name. It is repeated.

5:select depart,avg(salary) from emp where salary>10000 and sex=‘男’ group by depart;

Note: It should be noted here that the question requirement is to query male employees whose salary is greater than 10,000. This is filtered before grouping by department. In the filtered result set, the average salary of each department is then queried.


There are employee tables, department tables and salary tables. Write the corresponding SQL according to the query conditions [Tongcheng Yilong 2020 School Recruitment Written Test Questions]

Now there are employee table, department table and salary table. The fields of the department table department include department_id, name; the fields of the employee table staff include staff_id, name, age, depart_id; the fields of the salary table salary include salary_id, staff_id, salary, and month.

(Problem a): Find the total department salary of each department in the month '2016-09'

(Question b): Find the number of people in each department and require the output of the department name and number of people.

(Question c): Find the monthly salary expenditure of each department of the company, and require the output of the month and the total salary of this month.

select depart, sum(salary) from salary, staff 
    where staff.staff_id = staff.staff_id
    and month = '2016-09' group by depart_id;


select depart_name, count(staff_id) from depart, staff 
    where depart.depart_id = staff.depart_id 
    group by depart.depart_id;


select month, sum(salary) from salary, staff
    where salary.staff_id = staff.staff_id
    group by depart_id by month;

(Problem a): Find the total department salary of each department in the month '2016-09'

Answer:

SELECT

dep.NAME,

sum( sal.salary )

FROM

salary sal

JOIN staff sta ON sal.staff_id = sta.staff_id

JOIN depart dep ON sta.depart_id = dep.depart_id

WHERE

YEAR ( sal.MONTH ) = 2016

AND MONTH ( sal.MONTH ) = 9

GROUP BY

dep.depart_id

illustrate:

The functions of year and month in mysql are year (field) and month (field) respectively.

The query requires information about each department, so it must be grouped according to department. Departments and employees are 1:m, employees and salaries are 1:m. The query requires departments and salaries, so 3 tables must be associated for query

(Question b): Find the number of people in each department and require the output of the department name and number of people.

Answer:

SELECT

dep.NAME,

count( sta.staff_id )

FROM

staff sta

JOIN depart dep ON dep.depart_id = sta.depart_id

GROUP BY

sta.depart_id

Note: The information required by the query includes department and number of people. The number of people can only be obtained from the employee table, so the related table is the department table/employee table. Query the number of rows of employee IDs grouped by department IDs

(Question c): Find the monthly salary expenditure of each department of the company, and require the output of the month and the total salary of this month.

Answer:

SELECT

dep.NAME,

sal.MONTH,

sum( sal.salary )

FROM

depart dep

JOIN staff sta ON dep.depart_id = sta.depart_id

JOIN salary sal ON sta.staff_id = sal.staff_id

GROUP BY

dep.depart_id,

sal.MONTH

Note: According to the question requirements, the query information includes department/month/monthly total salary of each department. The salary information is in the salary table. Each employee can have multiple salary records (different times). Therefore, we need to group by department and then group by month to query the total salary of the grouped departments in different months.


Write the query conditions for the following database [SJTU Sinuo 2020 written examination questions]

There are two tables as follows:

表A(varchar(32) name, int grade)

Data: zhangshan 80, lisi 60, wangwu 84

表B(varchar(32) name, int age)

Data: zhangshan 26, lisi 24, wangwu 26, wutian 26

Write the SQL statement to get the following query results:

| NAME | GRADE | AGE |

| --------- | ----- | ---- |

| zhangshan | 80 | 26 |

| lisi | 60 | 24 |

| wangwu | 84 | 26 |

| wutian | null | 26 |

select a.name, a.grade, b,age from B left join A where A.name = B.name;

SELECT

B.NAME,

A.grade,

B.age

FROM

B left join A

ON

A.NAME = B.NAME

Note: It mainly examines whether to use inline or outer join when using related query. Here Wutian has no records in table A, but it still needs to return results, so table B should be used as the outer join query.


Write a SQL query to obtain the second highest salary (Salary) in the Employee table [Momo Technology 2020 School Recruitment Written Exam Questions]

Write a SQL query to obtain the second highest salary (Salary) in the Employee table [Momo Technology 2020 School Recruitment Written Exam Questions]

±----±------+

| Id | Salary|

±----±------+

| 1 | 100 |

±----±------+

| 2 | 200 |

±----±------+

| 3 | 300 |

±----±------+

For example, in the above Employee table, the SQL query should return 200 as the second highest salary. If the second highest salary does not exist, the query should return null.

±--------------------+

| SecondHighestSalary |

±--------------------+

| 200 |

±--------------------+

select salary as SecondHighestSalary from employee order by salary asc limit 1;

SELECT

max( Salary ) AS SecondHighestSalary

FROM

Employee

WHERE

Salary < ( SELECT max( Salary ) FROM Employee );

Note: The syntax supported by standard SQL is used here. First, find the maximum salary, then query the results of all salaries that are smaller than the maximum salary, and then query the maximum value among the results. You can also consider using simple paging operations, limit 1, 1. Note that limit is a keyword unique to mysql, not standard sql.


Query using UNION statement

It is known that the field definitions of T1 and T2 are exactly the same. T1 has 5 different pieces of data, and T2 has 5 different pieces of data. Among them, T1 has 2 pieces of data stored in table T2. Use the UNION statement to query these data and require that duplicate data does not appear.

select * from t1 union select * from t2;

SELECT * FROM T1 UNION SELECT * FROM T2

Note: Union cannot have duplicates, union all will have duplicate data. Use union according to the question requirements

Guess you like

Origin blog.csdn.net/qq_56884023/article/details/124463230