R
@Roopesh122
LEFT( ) & RIGHT( ) Functions
/* You manage an online event ticketing system where each ticket has a unique Ticket Code in the following format:
"<priority_code><region_code><ticket_number>"
• `priority_code`: First 3 characters (e.g., "HIG", "MED", "LOW").
• `region_code`: The next 2 characters (e.g., "NA", "EU", "AP").
• `ticket_number`: Remaining digits after the region code.
Input:
| ticket_id | ticket_title | ticket_code | ticket_status |
|-----------|-----------------------------|-----------
LENGTH( ) Function
/*
You are managing a database for a content platform where users submit articles.The `articles` table contains the following columns:
| Column Name | Data Type | Description |
|---------------|---------------|-----------------------------|
| article_id | INT | Unique ID for each article |
| title | VARCHAR(255) | Title of the article |
| content | TEXT | Main content of the article |
| language | VARCHAR(20)
CHAR_LENGTH( ) Function
/*
You are managing a system that tracks customer support tickets. The ticket details are stored in the `support_tickets` table,
which has the following schema:
| Column Name | Data Type | Description |
|----------------|---------------|-------------------------------------|
| ticket_id | INT | Unique ID for each support ticket |
| ticket_title | VARCHAR(255) | Title of the support ticket |
| ticket_status | VARCHAR(20) | St
SUBSTRING_INDEX( ) Function
/*
The `customers` table stores customers' full names in the `full_name` column,where names are stored
in the format `First Middle Last`.
Write a query to:
1. Extract only the first name from the `full_name` column.
2. Extract only the last name from the `full_name` column.
3. Extract the domain of the email address (everything after the `@`).
Input (customers table):
REPLACE( ) Function
/*
You are working with a `customers` table that contains the following columns:
| Column Name | Data Type | Description |
|-------------------|---------------|-----------------------------------------------------|
| `customer_id` | INT | Unique ID for each customer |
| `email_address` | VARCHAR(100) | Email addresses that may contain invalid characters |
| `phone_number` | VARCHAR(15) | C
TRIM( ) Function
/* You are working with a `products` table that contains the following columns:
| Column Name | Data Type | Description |
|---------------|-----------|------------------------------------------------------------------------------|
| `product_id` | INT | Unique ID for each product |
| `product_name`| VARCHAR | Product names that may have leading and trailing spac
SUBSTRING( ) Function
/* You are working with a `logs` table that contains the following columns:
| Column Name | Data Type | Description |
|--------------|-------------|--------------------------------------------------|
| `log_id` | INT | Unique ID for each log entry |
| `log_message`| VARCHAR(255)| A message that includes a timestamp in the format `YYYY-MM-DD HH:MM:SS - Message Content` |
| `log_type` | VARCHAR(50) | Type of the
CAST( ) Function
/*You are working with a sales table that contains the following columns:
sale_id INT: Unique identifier for each sale
sale_date DATE: The date when the sale occurred
sale_amount VARCHAR: Sale amount stored as a string
discount_rate VARCHAR: Discount rate stored as a percentage string
The sale_amount and discount_rate columns are stored as strings due to legacy system constraints.
Your task is to calculate the net revenue for each sale by performing the following:
CONCAT( ) FUNCTION
/*
Question:
You have a table orders with the following columns:
order_id (INT)
customer_name (VARCHAR)
order_date (DATE)
total_amount (DECIMAL)
status (VARCHAR: values can be "Pending", "Completed", "Canceled")
MIN() AND MAX() Function
/* You are working with a transactions table that contains the following columns:
transaction_id INT: Unique ID for each transaction
transaction_date DATE: Date of the transaction
amount INT: Amount of the transaction
Write a SQL query to determine the difference between the maximum and minimum transaction amounts for the current year. */
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY,
SUM() Function
/* You are working with a transactions table that contains the following columns:
transaction_id INT: Unique ID for each transaction
transaction_date DATE: Date of the transaction
amount INT: Amount of the transaction
Write a SQL query to calculate the total revenue generated for each month in the last year.
If no transactions occurred in a month, display that month with a total revenue of 0. */
CREATE TABLE transactions (