Aliasing
1. Aliasing
We're now going to discover how to make our query results much clearer for anyone to read.2. Confusing field names
When we work with databases, we may encounter field names that are difficult to understand or are poorly formatted. Consider our PoweredHomes database: a field might be named "prod_nm" instead of "product_name", or we might see "Customer ID" with capital letters and spaces, or even "cust Id" with inconsistent formatting. These variations can be confusing when we're trying to read and communicate our results.3. Alias
We can solve this readability issue by asking the AI assistant to create an alias for us. An alias is a custom label for a field in our query results. It is an alternative name that appears in our output, making results clearer without changing the actual database. Aliases are created with the AS keyword in our SELECT statement.4. Alias
Let's say we want to see customer first names but want to make the field names more descriptive. We might prompt: "Show me the customer names as first_name." The AI assistant interprets this request and generates SQL code using the AS keyword to create meaningful labels. This query result displays first_name instead of the original field name.5. Field naming
Here are some naming conventions we can follow to help us create effective aliases. Field names are usually written in lower-case with underscores instead of spaces, and use singular descriptors like "product_name" since each row represents an individual record. Fields must also have different names from their table and other fields; we wouldn't have a "products" field in our "products" table as this would make it difficult to tell the difference both for us and when working with an AI assistant.6. Table naming
Table names follow similar conventions but use plural descriptors like "products" or "customers" since tables hold multiple records. While we can't use aliasing to change table names, understanding these conventions helps us write more accurate prompts and interpret SQL queries and results.7. Aliasing with AI
Sometimes our prompts will require exact field names from the database schema, and other times we can describe what we want in everyday language. It's a good idea to define an alias precisely in the prompt, like the "first_name" example we saw. Developing this skill gives us precise control over how our AI assistant interprets our requests to generate SQL queries.8. Let's practice!
Time for some more practice.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.