# SQL (Intermediate)

## Weather Observation Station 5

[Question Link](https://www.hackerrank.com/challenges/weather-observation-station-5/problem)

Query the two cities in **STATION** with the shortest and longest *CITY* names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically. \
The **STATION** table is described as follows:

![](https://s3.amazonaws.com/hr-challenge-images/9336/1449345840-5f0a551030-Station.jpg)

where **LAT\_N** is the northern latitude and **LONG\_W** is the western longitude.

**Sample Input**

For example, **CITY** has four entries: **DEF, ABC, PQRS** and **WXY**.

**Sample Output**

```
ABC 3
PQRS 4
```

**Note** \
You can write two separate queries to get the desired output. It need not be a single query.

```sql
(SELECT
    CITY, LENGTH(CITY)
FROM STATION s
ORDER BY LENGTH(CITY), CITY
LIMIT 1)
UNION
(SELECT
    CITY, LENGTH(CITY)
FROM STATION s
ORDER BY LENGTH(CITY) DESC, CITY ASC
LIMIT 1)

```

## &#x20;                                                                   &#x20;

## Weather Observation Station 20

A [*median*](https://en.wikipedia.org/wiki/Median) is defined as a number separating the higher half of a data set from the lower half. Query the *median* of the *Northern Latitudes* (*LAT\_N*) from **STATION** and round your answer to  decimal places.&#x20;

**Input Format**

The **STATION** table is described as follows:

![](https://s3.amazonaws.com/hr-challenge-images/9336/1449345840-5f0a551030-Station.jpg)

where *LAT\_N* is the northern latitude and *LONG\_W* is the western longitude.

```sql
SELECT ROUND(LAT_N,4)
FROM (
  SELECT 
    LAT_N, 
    ROW_NUMBER() OVER (ORDER BY LAT_N) AS row_num,
    COUNT(*) OVER () AS total_rows
  FROM STATION
) AS subquery
WHERE row_num IN (FLOOR((total_rows + 1) / 2), CEIL((total_rows + 1) / 2));

```

## &#x20;                                                                   &#x20;

## New Companies

[Question Link](https://www.hackerrank.com/challenges/the-company/problem)

Each of the companies follows this hierarchy:

<figure><img src="https://483934582-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F332OmkaCCBc9TZFZXfnO%2Fuploads%2FkyFSyI6O5gVE0NffUr9W%2F1458531031-249df3ae87-ScreenShot2016-03-21at8.59.56AM.png?alt=media&#x26;token=4334f629-476f-466c-887a-7e4999e58b3d" alt=""><figcaption></figcaption></figure>

Write a query to print the company\_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company\_code.

**Note:**

* The tables may contain duplicate records.
* The company\_code is string, so the sorting should not be **numeric**. For example, if the company\_codes are C\_1, C\_2, and C\_10, then the ascending company\_codes will be C\_1, C\_10, and C\_2

**Sample Input**

Company Table:

<div align="center" data-full-width="false"><figure><img src="https://miro.medium.com/v2/resize:fit:464/0*LDe0sAdb5TpIyWwZ.png" alt="" height="117" width="232"><figcaption></figcaption></figure></div>

Lead\_Manager Table:

<figure><img src="https://miro.medium.com/v2/resize:fit:598/0*EQyLbLXERCCWbiDi.png" alt="" height="116" width="299"><figcaption></figcaption></figure>

Senior\_Manager Table:

<figure><img src="https://miro.medium.com/v2/resize:fit:956/0*vnKtj7SuFswfFDPL.png" alt="" height="152" width="478"><figcaption></figcaption></figure>

Manager Table:

<figure><img src="https://miro.medium.com/v2/resize:fit:1208/0*N43-oGYx3OGm1FBv.png" alt="" height="152" width="604"><figcaption></figcaption></figure>

Employee Table:

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/0*o9oTd1ObyKvG0p_o.png" alt="" height="178" width="700"><figcaption></figcaption></figure>

**Sample Output**

```
C1 Monika 1 2 1 2
C2 Samantha 1 1 2 2
```

**Solution:**

```sql
SELECT
    e.company_code,
    c.founder,
    COUNT(DISTINCT e.lead_manager_code) AS ld_mngr_cnt,
    COUNT(DISTINCT e.senior_manager_code) AS snr_mngr_cnt,
    COUNT(DISTINCT e.manager_code) AS mngr_cnt,
    COUNT(DISTINCT e.employee_code) AS emp_cnt
FROM employee e
JOIN company c 
ON e.company_code = c.company_code
GROUP BY e.company_code, c.founder
ORDER BY e.company_code
```

Link to[ Full Output](https://hr-testcases-us-east-1.s3.amazonaws.com/19505/output000.txt?response-content-type=text%2Fplain\&X-Amz-Algorithm=AWS4-HMAC-SHA256\&X-Amz-Credential=AKIAR6O7GJNX5DNFO3PV%2F20230605%2Fus-east-1%2Fs3%2Faws4_request\&X-Amz-Date=20230605T214559Z\&X-Amz-Expires=7200\&X-Amz-SignedHeaders=host\&X-Amz-Signature=f1e8cbde7ee433ed321c4258ef5194a6a4c1d1c3da61fde648339f5ccbd0a9b3)
