SQL Challenge One: Find all information for the latest onboarding staff

Source: Internet
Author: User

The above database tables:

CREATE TABLE ' Employees ' (
' Emp_no ' int (one) is not NULL,
' Birth_date ' date not NULL,
' first_name ' varchar (not NULL),
' last_name ' varchar (+) not NULL,
' Gender ' char (1) Not NULL,
' Hire_date ' date not NULL, ------------onboarding time
PRIMARY KEY (' emp_no '));

SQL: Query All information about the latest onboarding staff

This problem has a pit ( cautious), consider the logical thinking, there are two query syntax, for different data environments.

Environment One : The latest entry staff only one person, that is, the single data of the day

---if there is only one person, then select Sort by condition (descending) to extract (limit) the first one. Limit two methods of use, (limit x) The first directly set the value, the value of how much to extract, (limit x, Y) the second is to start from a location to extract how many bars.

Use order by (the default is ascending) to unify the rows, and then conditional sort desc (Descending)/ASC (Ascending), order by is necessary, conditions are selected. Then the data can be extracted with limit.

SELECT * FROM Employees ORDER by ID (field name) DESC/ASC limit x (extract quantity)

Select *  from Order  by desc 0,1
Select *  from Order  by desc 1

Both of these methods are the same result (only for the above query criteria). when there is more than one data, the initial value of the fetch position becomes a different result.

Environment II : Number of people at the latest date of entry;

With the Where condition and subquery, the maximum value of the query field (max) can be

SELECT * from employess WHERE id = (select max (hire_date) from employess)

Select *  from where = (selectmax from employess)

SQL Challenge One: Find all information for the latest onboarding staff

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.