AI_SENTIMENT

AI_SENTIMENT

Overview

Extracts sentiment from text, returning a score from -1 (negative) to 1 (positive).

Syntax

AI_SENTIMENT(text)

Parameters

  • text (VARCHAR): Text to analyze for sentiment (max 2,048 tokens)

Use Cases

  • Customer feedback analysis
  • Social media monitoring
  • Product review analysis
  • Brand sentiment tracking
  • Support ticket prioritization

Code Examples

Example 1: Basic Sentiment Analysis

SELECT 
    'I love this product!' AS text,
    AI_SENTIMENT('I love this product!') AS sentiment_score;

Output:

text                    | sentiment_score
------------------------|----------------
I love this product!    | 0.89

Example 2: Analyze Customer Reviews

SELECT 
    review_id,
    review_text,
    AI_SENTIMENT(review_text) AS sentiment,
    CASE 
        WHEN AI_SENTIMENT(review_text) > 0.5 THEN 'Positive'
        WHEN AI_SENTIMENT(review_text) < -0.5 THEN 'Negative'
        ELSE 'Neutral'
    END AS sentiment_category
FROM customer_reviews;

Example 3: Track Sentiment Over Time

SELECT 
    DATE_TRUNC('week', created_date) AS week,
    AVG(AI_SENTIMENT(review_text)) AS avg_sentiment,
    COUNT(*) AS review_count
FROM product_reviews
WHERE created_date >= CURRENT_DATE - 90
GROUP BY week
ORDER BY week;

Example 4: Compare Sentiment by Product

SELECT 
    product_id,
    product_name,
    AVG(AI_SENTIMENT(review_text)) AS avg_sentiment,
    COUNT(CASE WHEN AI_SENTIMENT(review_text) > 0.5 THEN 1 END) AS positive_reviews,
    COUNT(CASE WHEN AI_SENTIMENT(review_text) < -0.5 THEN 1 END) AS negative_reviews
FROM reviews
GROUP BY product_id, product_name
ORDER BY avg_sentiment DESC;

Example 5: Filter Negative Feedback

SELECT 
    ticket_id,
    customer_message,
    AI_SENTIMENT(customer_message) AS sentiment_score
FROM support_tickets
WHERE AI_SENTIMENT(customer_message) < -0.3
  AND status = 'Open'
ORDER BY sentiment_score ASC;

Data Output Examples

Sentiment Scores

Text: "This is the best product I've ever used!"
Score: 0.95 (very positive)

Text: "The product is okay, nothing special."
Score: 0.15 (slightly positive/neutral)

Text: "Terrible experience, would not recommend."
Score: -0.87 (very negative)

Sentiment Distribution

Product: Laptop Model X
Positive Reviews (>0.5): 245 (68%)
Neutral Reviews (-0.5 to 0.5): 85 (24%)
Negative Reviews (<-0.5): 30 (8%)
Average Sentiment: 0.42

Model Information

  • Model Used: Snowflake managed model
  • Context Window: 2,048 tokens
  • Output Range: -1.0 to 1.0
  • Supported Languages: Primarily English (check docs for multilingual support)

Limitations & Considerations

Input Size

  • Maximum 2,048 tokens per text
  • Longer texts will be truncated
  • Use AI_COUNT_TOKENS to verify length

Cost

  • Input tokens are billable
  • Additional prompt overhead added automatically
  • Use sparingly on very large datasets

Accuracy

  • Works best on clear, straightforward sentiment
  • May struggle with sarcasm or complex emotions
  • Context matters for accurate scoring

Regional Availability

  • AWS US West/East: ✓
  • Azure East US: ✓
  • EU regions: ✓
  • Cross-region inference: ✓

Best Practices

1. Categorize Sentiment Scores

-- Create meaningful categories
SELECT 
    CASE 
        WHEN AI_SENTIMENT(text) >= 0.6 THEN 'Very Positive'
        WHEN AI_SENTIMENT(text) >= 0.2 THEN 'Positive'
        WHEN AI_SENTIMENT(text) >= -0.2 THEN 'Neutral'
        WHEN AI_SENTIMENT(text) >= -0.6 THEN 'Negative'
        ELSE 'Very Negative'
    END AS sentiment_category,
    COUNT(*) AS count
FROM feedback
GROUP BY sentiment_category;

2. Combine with Other Metrics

SELECT 
    product_id,
    AVG(rating) AS avg_rating,
    AVG(AI_SENTIMENT(review_text)) AS avg_sentiment,
    CORR(rating, AI_SENTIMENT(review_text)) AS rating_sentiment_correlation
FROM reviews
GROUP BY product_id;

3. Monitor Sentiment Trends

CREATE VIEW sentiment_trends AS
SELECT 
    DATE_TRUNC('day', created_at) AS date,
    product_category,
    AVG(AI_SENTIMENT(feedback_text)) AS avg_sentiment,
    STDDEV(AI_SENTIMENT(feedback_text)) AS sentiment_volatility
FROM customer_feedback
GROUP BY date, product_category;

4. Alert on Negative Sentiment Spikes

WITH daily_sentiment AS (
    SELECT 
        DATE(created_at) AS date,
        AVG(AI_SENTIMENT(comment)) AS avg_sentiment
    FROM social_media_mentions
    WHERE brand = 'OurBrand'
    GROUP BY date
)
SELECT *
FROM daily_sentiment
WHERE avg_sentiment < -0.3
  AND date >= CURRENT_DATE - 7
ORDER BY date DESC;

Common Use Cases

Customer Service Prioritization

SELECT 
    ticket_id,
    customer_email,
    AI_SENTIMENT(ticket_description) AS sentiment,
    CASE 
        WHEN AI_SENTIMENT(ticket_description) < -0.6 THEN 'High Priority'
        WHEN AI_SENTIMENT(ticket_description) < -0.2 THEN 'Medium Priority'
        ELSE 'Normal Priority'
    END AS priority
FROM support_tickets
WHERE status = 'Open';

Product Launch Monitoring

SELECT 
    launch_date,
    product_name,
    AVG(AI_SENTIMENT(social_mention)) AS avg_sentiment,
    COUNT(*) AS mention_count
FROM social_media_monitoring
WHERE product_name = 'New Product X'
  AND mention_date BETWEEN launch_date AND launch_date + 30
GROUP BY launch_date, product_name;

Related Functions

  • AI_CLASSIFY - For categorical sentiment classification
  • AI_COMPLETE - For detailed sentiment analysis with reasoning
  • AI_AGG - For aggregating sentiment insights

Interested to Learn More? Try Out the Free 14-Days Trial

One AI-native workspace to build, ship, and monitor dbt™ — without the tool sprawl.