Literal Values, Data Types, and Type Casting in SQL
Every value in a PostgreSQL query has a type, and PostgreSQL uses that type to determine what operations are valid and how expressions are evaluated. Literal values and type casting are the mechanisms by which you introduce and convert typed values within a query.
Every value in SQL has a type, and that type determines what SQL can do with it.
When you write 48000 * 0.075, SQL already knows what it's working with: both values are numeric, so the multiplication works. But sometimes a value arrives as the wrong type. A date stored as text. A price stored as a string. SQL won't treat those as numbers or dates on its own. You have to tell it what type a value really is. The :: operator handles this — it's called a cast.
Think of :: as saying "treat this as." You put it after the value, then write the target type. A text string like '2025-06-30' is just characters until you add ::date:
SELECT '2025-06-30'::date AS contract_end_dateNow SQL treats it as a real date. The ::date cast is what makes that switch.
You can cast between many types. Text to number: '199.99'::numeric. Number to text: 42::text. These come up regularly when data is imported from spreadsheets or external systems where everything lands as a string.
SQL also has a second casting syntax that does the same thing:
SELECT CAST('2024-09-01' AS date) AS launch_date:: is shorter and more common in PostgreSQL. CAST() is standard SQL and appears in other databases. Both produce the same result.
SQL also has type-prefixed literals for some types. An interval — a duration of time — is written as INTERVAL '30 days'. The type name comes before the quoted value instead of after it.
The one thing that trips people up: integer division drops the decimal.
Divide an integer by an integer and SQL returns an integer, cutting off anything after the decimal point. '10000'::integer / 4 returns 2500, not 2500.0. Cast to ::numeric first and you get the full decimal result. The same fix works whenever you need the fractional part of a division: make at least one side a decimal type.
9 Literal Values, Data Types, and Type Casting practice problems
Write a query to convert that text string into a date.
Write a query to convert it.
Write a query to convert it.
Write a query to return the total annual cost for a 12-month subscription.
Write a query to return the equal quarterly share for one department.
Write a query to return the trial duration as an interval.
Write a query to return it as a calendar date.
Write a query to return the exact per-department budget.
Write a query to return how many whole people end up per group.
Start free to practice all 9 Literal Values, Data Types, and Type Casting problems, with instant grading and mastery tracking.
Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.
That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.
Real problems. Not toy examples.
615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.
Write a query. Know if it's right in one second.
No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.
Stop guessing whether you actually know it.
SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.
Learn it once. Keep it for good.
Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.