/

/

How to Write Basic SQL Statements

HOW TO

How to Write Basic SQL Statements

How to Write Basic SQL Statements

How to Write Basic SQL Statements

May 4, 2024

May 4, 2024

May 4, 2024

If you're interested in SQL for beginners, you might find yourself having to get to grips with SQL querying. It can seem a tough task, but in this blog post, we'll take you through the basics. By the end of it, you'll have a good understanding of SQL querying, and it will be a lot less intimidating than it currently looks.

Many ask if they really need to learn SQL when BI (Business Intelligence) tools are becoming more sophisticated. 

To a certain extent, yes. While these tools can generate SQL queries for you, they only cover basic ones used for generating charts, graphs, and visuals for your dashboards. For more advanced tasks such as pre-aggregating, filtering, or selecting specific data for your project, you'll need to write SQL queries.


Understanding SQL Statements

We start by understanding SQL statements. Our aim is to get the database to give us specific data, and to do this, we use a select statement. If we want to return all data in a table, we simply say select * from [table name].

If we only want certain fields or columns, then we specify the fields we want instead of using *. We do this by saying select [field name] from [table name]. For example, to get user names and date of birth from a table named users, we say select user_name, DOB from users.

To make it easier to understand the output, we can rename fields by creating aliases. We do this using the as function. So, to rename "user_name" to just "name", we say select user_name as name from users.



Getting Specific with Data Retrieval

Often times, we don't want to retrieve every data row available. Instead, we just need some specific rows. To do this, we use a where clause along with an operator. For example, if we only wanted the data of users whom age less than 30, we simply say select * from [table name] where age < 30.

We can use multiple conditions by using and or or. For instance, select * from users where age > 30 and height > 180 returns users who age bigger than 30 and are taller than 190.

We can use similar logic for retrieving data based on text values. For example, select * from users where user_name = 'John Doe' would only return data for the user named John Doe. Likewise, if we only wish to retrieve data from all users named John, we can use the like operator, which works with percentage signs (%) to represent all possible characters. So we would say select * from users where user_name like 'John%'.


Additional Operators

There are more operators we can use when specifying the rows we want to return. in allows us to specify multiple values for a certain field. So if we wanted to retrieve data for either John Doe or Lena Brown, we would write select * from users where user_name in ('John Doe', 'Lena Brown').

We can also get sections of our data with the between operator. For instance, if we wish to retrieve data for users who age between 30 and 40, we would write select * from users where age between 30 and 40.

To check for empty fields, we use the null and not null conditions. For instance, select * from users where user_id is null would return all data with a null value in the user_id field.


Arranging our Data

We can sort our data using an order by clause. By default, data is sorted in ascending order unless DESC is specified. For example, to sort by age in descending order, we'd say select * from users order by age DESC.


Joining Data from Different Tables

Suppose we have another table, user attributes, which doesn't have the user’s name. But we want a view containing the user’s name and their comment quantity. In this case, we would use a join. Assume the fields desired are user_id and comment_count. We would put select user_attributes.user_id, users.user_name, user_attributes.comment_count....


Use AI SQL Query Writer to learn fast

This post has covered the basics of SQL querying - how to select specific fields, how to filter and sort your data, and how to join data from different tables. We hope this makes SQL a little less daunting for you! However, we can't ignore the steep learning curve and the time it consumes. Now let's talk about a solution that can ease this burden and streamline your SQL querying process.


AI2SQL is an AI SQL query builder. In a succinct manner, it writes a query from text, analyzes your query, and notably, improves both your performance and that of the SQL query. With AI2SQL, you don’t need to be bogged down by the nitty-gritty details of SQL querying syntax or worry about the dessert-dry runs of digging up the right data.



Share this

More Articles

More Articles

More Articles