Database face test--match data query

Source: Internet
Author: User

This interview is very special, gave the test paper, let the direct oral answer, to tell the truth, quite like this.

The last is a database problem, the table is as follows



I yy the initialization statement

Mysql> CREATE TABLE Fmatch (mdate date, Team varchar, res enum (' WIN ', ' lose '));mysql> insert into fmatch values (' 2 010-10-19 ', ' 76ers ', ' WIN '); Query OK, 1 row affected (0.07 sec) mysql> insert into fmatch values (' 2010-10-20 ', ' 76ers ', ' lose '); Query OK, 1 row affected (0.07 sec) mysql> insert into fmatch values (' 2010-10-20 ', ' NET ', ' WIN '); Query OK, 1 row affected (0.04 sec) mysql> insert into fmatch values (' 2010-10-21 ', ' NET ', ' WIN '); Query OK, 1 row affected (0.04 sec)

Here are the questions

1. Identify the earliest day and last day data

That


Idea: Max, min

Answer:

SELECT * from Fmatch where mdate in (select Max (mdate) from Fmatch) or  mdate in (select min (mdate) from Fmatch);

This question is relatively simple, but if you have a better answer, welcome to share!


The second question is more difficult.

2. Statistics on the outcome of each team


I didn't think of it on the spot. An SQL statement does not solve the problem if you want to use the IF statement. Oh, that's so stupid.

Back on the internet to find out a very close answer

Select A.team, A.win,b.lose from (SELECT COUNT (*) win, team from Fmatch where res= ' win ' Group by team) a left join  (s Elect Count (*) lose, team from Fmatch where res= ' lose ' GROUP by team] B on A.team=b.team;


There are still flaws, however, when the data is not displayed as Null instead of 0.

There is the interview when the interviewer gave the hint is the ranks of the transformation, really need to use the ranks of the transformation??

How to solve??? Help you netizens!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Database face test--match data query

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.