These few SQL interview questions kill most of the graduates with 0 years of work experience

Recently, a friend sent a few SQL questions, saying that they encountered them during the interview. Take a look at the questions, it is indeed different from the general SQL questions, there are still some tricks, for novices with no work experience, a small part of them can write one question, and some of them can write two questions. too much.

However, for programmers with work experience, at least 3 of these questions are not a problem.
If you are a classmate who just graduated, you happen to encounter these questions in the interview, and you happen to know how to do it, then congratulations, with these few SQL questions, you may pass the interview 90% of the time.

Don't talk nonsense, let's start with the question:

Question 1: Use a SQL statement to query records that are not pure numbers in a field (A) in the business table (test_1)

Question 2: Use a sentence of SQL to query the product sales ranking table (test_2) from the third to the sixth product name and sales
data: product sales ranking test_2 (product number: pro_id, sales volume: sales_volume)
product Table test2_pro (product number: pro_id, product name pro_name)

Question 3: Use a sentence of SQL to delete the duplicate data in the c field in the table according to the primary key (table: test_3; primary key: two fields a and b, and the field c is varchar)

Question 4: Use a sentence of SQL to summarize the data of the business table (test_4) (Figure 1), and summarize the payment amount for each store and payment method on a daily basis, and use a sentence of SQL to convert it into the format shown in Figure 2.

===================================================================================================================================================
_

Let me make some complaints before giving a lecture. SQL questions are disgusting. If you have no experience, you must first construct the data. Otherwise, even if you write it out, you can’t guarantee that what you wrote must be correct. If the construction is not level, even if the execution result of your SQL statement is correct, the SQL statement may not be correct.

Some students may not understand the above passage, so let’s ignore it for now. On a certain day in a certain year and a certain month, you may encounter a SQL statement that was executed correctly yesterday, but it is wrong today. At that time, you may understand that yesterday's execution result was correct, because it happened that yesterday's wrong data happened to encounter yesterday's wrong SQL, and finally got a correct result. so. . . .

Well, this time I helped you build the data. If you want to do SQL questions, you must practice them yourself in order to practice your real skills. Therefore, I will not give the answers directly in this article. The questions are not difficult. I hope you can solve them yourself. To practice, spend a little time to find the answer yourself, and you can also reap the joy of solving the problem. If you still have doubts, you can also go back to the article, the way to get the answer is at the end of the article.

CREATE TABLE `test_1` (
  `a` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `test_1` (`a`) VALUES ('abcd1234');
INSERT INTO `test_1` (`a`) VALUES ('1234');
INSERT INTO `test_1` (`a`) VALUES ('1234abcd');
INSERT INTO `test_1` (`a`) VALUES ('abcd');
INSERT INTO `test_1` (`a`) VALUES ('<>?:"');
INSERT INTO `test_1` (`a`) VALUES ('abcd1234aaaa');
INSERT INTO `test_1` (`a`) VALUES ('abcd1234aa234234aa');

CREATE TABLE `test_2` (
  `pro_id` int(11) NOT NULL,
  `sales_volume` int(11) NOT NULL,
  PRIMARY KEY (`pro_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (1, 7);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (2, 8);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (3, 1);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (4, 9);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (5, 10);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (6, 6);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (7, 4);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (8, 5);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (9, 3);
INSERT INTO `test_2` (`pro_id`, `sales_volume`) VALUES (10, 2);

CREATE TABLE `test2_pro` (
  `pro_id` int(11) NOT NULL,
  `pro_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`pro_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (1, 'Mate20');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (2, 'Mate20Pro');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (3, 'Mate30');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (4, 'Mate30Pro');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (5, 'Mate40');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (6, 'Mate40Pro');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (7, 'Mate50');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (8, 'Mate50Pro');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (9, 'P10');
INSERT INTO `test2_pro` (`pro_id`, `pro_name`) VALUES (10, 'P20');


CREATE TABLE `test_3` (
  `a` int(11) NOT NULL,
  `b` int(11) NOT NULL,
  `c` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`a`,`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test_3` (`a`, `b`, `c`) VALUES (1, 1, 'Tom');
INSERT INTO `test_3` (`a`, `b`, `c`) VALUES (1, 2, 'Tom');
INSERT INTO `test_3` (`a`, `b`, `c`) VALUES (2, 1, 'Jarry');
INSERT INTO `test_3` (`a`, `b`, `c`) VALUES (2, 2, 'Martin');
INSERT INTO `test_3` (`a`, `b`, `c`) VALUES (2, 3, 'Amy');

CREATE TABLE `test_4` (
  `单号` varchar(255) DEFAULT NULL,
  `门店` varchar(255) DEFAULT NULL,
  `结单日期` datetime DEFAULT NULL,
  `支付方式` varchar(255) DEFAULT NULL,
  `支付金额` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020001', '1001', '2022-08-15', '现金', 3);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020002', '1002', '2022-08-14', '支付宝', 4);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020003', '1003', '2022-08-15', '微信', 7);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020004', '1001', '2022-08-14', '银行卡', 6);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020005', '1001', '2022-08-15', '现金', 8);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020006', '1003', '2022-08-14', '微信', 12);
INSERT INTO `test_4` (`单号`, `门店`, `结单日期`, `支付方式`, `支付金额`) VALUES ('2020007', '1004', '2022-08-15', '银行卡', 13);


Let's talk about the first question first:

Use a SQL statement to query records that are not pure numbers in a field (A) in the business table (test_1)

This question should not be difficult at first glance, but under normal circumstances, it is impossible to use traditional SQL statements, that is to say, if you want to use LIKE to check, I'm sorry, it can't be done. At this time, regular expressions must be used. Go search for regular usage.

Let's look at the second question:

Use a sentence of SQL to query the product sales ranking table (test_2) from the third to the sixth product name and sales
data: product sales list test_2 (product number: pro_id, sales volume: sales_volume)
product table test2_pro(product Number: pro_id, product name pro_name)

This question is not difficult, and graduate students who can learn can also do it. There are two test points.
One is the table connection, and the other is the usage of limit, which will not be described in detail.

then the third question

Use a sentence of SQL to delete the duplicate data in the c field in the table according to the primary key (table: test_3; primary key: a, b two fields, c field is varchar)

This question is a bit difficult. You can find many posts on the Internet to delete duplicate data, but 99% of them are one primary key. Two primary keys for this question are a bit troublesome, but the ideas are similar.
For example, this idea:

delete from Student
  where Name in( select Name from Student group by  Name having count(Name) > 1) and 
 ID not in(select  max(ID) from Student group by  Name having count(Name) > 1 )

Use a subquery to find out the duplicate Name list, and then use name in () to select the records to be deleted.
Then use an id not in () to keep the one with the largest id value among the duplicate data, and delete the others.

And we know that when using the IN subquery, there can only be one column in the subquery, but what should we do if we need to match multiple columns? This is how to do?

In fact, the problem came out, the subquery can only support one column, and the idea is already there, that is, it is OK to change multiple columns into one column. How to become a column?
There are many ways, such as using concat to connect two columns, but you should pay attention, if there are two records, a and b columns are 11, 2, and 1, 12 respectively. If you do not deal with it, it will affect the execution result. How to solve this problem? Use your brains.

Final fourth question:

Use a sentence of SQL to summarize the data of the business table (test_4) (Figure 1), and summarize the payment amount for each store and each payment method on a daily basis, and use a sentence of SQL to convert it into the format of Figure 2.

In fact, this SQL interview question is quite classic, the typical row to column, many novices are confused. There are two common solutions to this problem:
one is the subquery method, and the other is the case method. Both SQL statements seem to be quite frustrating.
Today I will introduce another SUM(IF()) method in MYSQL.

select 门店,结单日期,sum(if(支付方式='现金',t.支付金额,0)) as 现金
from test_4 t 
group by 门店,结单日期
order by 结单日期,门店

==================================================== ==
Actually, there is no shortcut to learning SQL, the best way is to practice. When practicing, you should first analyze the problem thoroughly, and then disassemble the complex problem, and then test and solve it step by step. Even a 3000-line SQL statement can be written.

In order to prevent everyone from being lazy, I uploaded the answer to the CSDN resource, click the link to download:

https://download.csdn.net/download/aley/86401105
requires 1 point for downloading, so you have to work hard to learn knowledge. If you want to scold me, please complain in the comments. Ha ha.

Students who have no points can also go to the VX official account to search [Yao SIR interview room], pay attention to it and reply "SQL interview question 01", and you can also get the answer.

Guess you like

Origin blog.csdn.net/aley/article/details/126349751