5. Add a column to the query described in exercise 2 that uses the RANK() function to return a column named BalanceRank that ranks the balance due in descending order.
SQL
SQL stands for Structured Query Language, is a form of communication that uses queries structured in a specific format to store, manage & retrieve data from a relational database.
Queries
A query is a type of computer programming language that is used to retrieve data from a database. Databases are useful in a variety of ways. They enable the retrieval of records or parts of records, as well as the performance of various calculations prior to displaying the results. A search query is one type of query that many people perform several times per day. A search query is executed every time you use a search engine to find something. When you press the Enter key, the keywords are sent to the search engine, where they are processed by an algorithm that retrieves related results from the search index. Your query's results are displayed on a search engine results page, or SER.
SQL server
Answer question 5
![## Exercises
1. **Select Statement for Vendor Contact Information:**
- Write a `SELECT` statement that returns two columns based on the Vendors table. The first column, Contact, is the vendor contact name in this format: first name followed by last initial (for example, “John S.”). The second column, Phone, is the VendorPhone column without the area code. Only return rows for those vendors in the 559 area code. Sort the result set by first name, then last name.
2. **Invoice Number and Balance Due:**
- Write a `SELECT` statement that returns the InvoiceNumber and balance due for every invoice with a non-zero balance and an InvoiceDueDate that’s less than 30 days from today.
3. **Modify Search Expression for Invoice Due Date:**
- Modify the search expression for InvoiceDueDate from the solution for exercise 2. Rather than 30 days from today, return invoices due before the last day of the current month.
4. **Summary Query Using the CUBE Operator:**
- Write a summary query that uses the `CUBE` operator to return LineItemSum (which is the sum of InvoiceLineItemAmount) grouped by Account (an alias for AccountDescription) and State (an alias for VendorState). Use the `CASE` and `GROUPING` function to substitute the literal value “*ALL*” for the summary rows with null values.
5. **Add RANK() to Existing Query:**
- Add a column to the query described in exercise 2 that uses the `RANK()` function to return a column named BalanceRank that ranks the balance due in descending order.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe5233eaf-2b59-4783-9ef6-5e04effc2c39%2F5511e7ee-6035-44a9-be7b-5e2d5190d198%2Fnx1lx3_processed.png&w=3840&q=75)
![**Step 2**
Write a SELECT statement that returns two columns based on the Vendors table. The first column, Contact, is the vendor contact name in this format: first name followed by the last initial (for example, "John S."). The second column, Phone, is the VendorPhone column without the area code. Only return rows for those vendors in the 559 area code.
**SQL Server Code:**
```sql
select VendorContactFName + ' ' + left(VendorContactLName,1) + '.'
as Contact, RIGHT(VendorPhone,8) as Phone
from Vendors
where substring(vendorphone,2,3)=559
order by VendorContactFName, VendorContactLName;
```
**Explanation:**
- The first column, Contact, is the vendor contact name in this format.
- The second column, Phone, is the VendorPhone column without the area code.
- The 4th line ensures it only returns rows for those vendors in the 559 area code.
- The last line sorts the result set by first name, then last name.
**Code Screenshot:**
```sql
select VendorContactFName + ' ' + left(VendorContactLName,1) + '.'
as Contact, RIGHT(VendorPhone, 8) as Phone
from Vendors
where substring(vendorphone,2,3)=559
order by VendorContactFName, VendorContactLName;
```](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe5233eaf-2b59-4783-9ef6-5e04effc2c39%2F5511e7ee-6035-44a9-be7b-5e2d5190d198%2Frsoonqs_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
SQL Server
Please help answer question 5
Please type out the answer because I cannot read the last answer because the handwriting is not very clear. Thank you.
![## Exercises
1. **Query for Vendor Contact and Phone**
- Write a `SELECT` statement that returns two columns from the Vendors table.
- The first column, `Contact`, should be formatted as the vendor's first name followed by the last initial (e.g., "John S.").
- The second column, `Phone`, should display the VendorPhone without the area code.
- Return rows only for vendors with a 559 area code.
- Sort the results by first name, then last name.
2. **Query for Invoice Details**
- Write a `SELECT` statement to fetch the `InvoiceNumber` and `balance due` for every invoice with:
- A non-zero balance.
- An `InvoiceDueDate` that is less than 30 days from today.
3. **Modify InvoiceDueDate Search Expression**
- Alter the search expression from exercise 2 so that invoices due before the last day of the current month are returned, rather than those due in the next 30 days.
4. **Summary Query using CUBE Operator**
- Create a query using the `CUBE` operator to return `LineItemSum` (from `InvoiceLineItemAmount`) grouped by:
- `Account` (alias as `AccountDescription`).
- `State` (alias as `VendorState`).
- Use the `CASE` and `GROUPING` functions to replace null values with the literal `*ALL*`.
5. **Add RANK Column to Query**
- Enhance the query from exercise 2 by adding a column that employs the `RANK()` function.
- This should create a column named `BalanceRank` to rank the balance due in descending order.](https://content.bartleby.com/qna-images/question/e5233eaf-2b59-4783-9ef6-5e04effc2c39/e01ca99d-d5fd-41ad-8126-3a400c7d92f9/0xlcsfn_thumbnail.png)
![### Step 2
**Task:**
Write a SELECT statement that returns two columns based on the Vendors table. The first column, Contact, is the vendor contact name in this format: first name followed by last initial (for example, "John S."). The second column, Phone, is the VendorPhone column without the area code. Only return rows for which the area code is 559.
**SQL Server Code:**
```sql
select VendorContactFName + ' ' + left(VendorContactLName, 1) + '.'
as Contact, RIGHT(VendorPhone, 8) as Phone
from Vendors
where substring(vendorphone, 2, 3) = 559
order by VendorContactFName, VendorContactLName;
```
**Explanation:**
- The first column, Contact, is the vendor contact name formatted as first name with last initial.
- The second column, Phone, shows the VendorPhone without the area code.
- The 4th line ensures the query returns only rows where the area code is 559.
- The last line orders the result set by first name, then last name.
**Code Screenshot:** (Note: The code is shown again as an image with SQL formatting applied for easy reference.)](https://content.bartleby.com/qna-images/question/e5233eaf-2b59-4783-9ef6-5e04effc2c39/e01ca99d-d5fd-41ad-8126-3a400c7d92f9/vq9p0jt_thumbnail.png)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)