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