Interview Questions, Answers and Tutorials

Writing Your First SQL Query

Writing Your First SQL Query

Welcome to the world of SQL! SQL (Structured Query Language) is a language used to communicate with databases. Imagine a database as a big, organized library full of information. SQL is the tool you use to ask questions and get the exact information you need from that library.

In this guide, we will cover:

  • What SQL is and why it’s important
  • How to write your first SQL query
  • Examples of simple SQL queries
  • Practice questions and interview questions with solutions

1. What is SQL?

SQL stands for Structured Query Language. It’s the language we use to talk to a database. We can use SQL to:

  • Ask the database to show us certain information (called “queries”)
  • Insert new information into the database
  • Update existing information
  • Delete information we no longer need

2. Database Basics

A database is like a giant spreadsheet or a big digital filing cabinet. The information in the database is stored in tables, which are like rows and columns in a spreadsheet. Each table has:

  • Columns: These define the type of information, like names, dates, or numbers.
  • Rows: These contain the actual data, like individual records or pieces of information.

For example, a table might look like this:

IDNameAgeCity
1John23New York
2Alice29Los Angeles
3Bob35Chicago

3. Writing Your First SQL Query

The most basic and important SQL query is the SELECT query. This is how we ask the database to show us some data.

Here’s the basic structure of a SELECT query:

SELECT column1, column2 FROM table_name;

Let’s break this down:

  • SELECT tells the database to show us the data.
  • column1, column2 are the names of the columns you want to see. You can select one or more columns.
  • FROM table_name specifies which table to get the data from.

Example 1: Get All Data from a Table

If you want to see all the data in a table, use this query:

SELECT * FROM table_name;

The asterisk (*) means “all columns.”

For example, using our earlier table:

SELECT * FROM people;

This would return:

IDNameAgeCity
1John23New York
2Alice29Los Angeles
3Bob35Chicago

Example 2: Get Specific Columns

If you only want to see certain columns, you can specify them like this:

SELECT Name, Age FROM people;

This would return:

NameAge
John23
Alice29
Bob35

4. Filtering Data with WHERE

Sometimes, we don’t want all the data. We only want rows that meet a specific condition. This is where the WHERE clause comes in.

Here’s how it works:

SELECT column1, column2 FROM table_name WHERE condition;

Example 3: Get Data Based on a Condition

If you want to find people who are older than 25, you can write:

SELECT Name, Age FROM people WHERE Age > 25;

This would return:

NameAge
Alice29
Bob35

5. Practice Questions

Now that you know the basics, it’s time to practice! Try writing SQL queries for the following questions:

Question 1: Select All Columns from the “Students” Table

Write a query to select all the data from a table called Students.

Solution:

SELECT * FROM Students;

Question 2: Select Only Names and Ages from the “Employees” Table

Write a query to select only the Name and Age columns from the Employees table.

Solution:

SELECT Name, Age FROM Employees;

Question 3: Get the Names of People Who Live in “Chicago”

Write a query to select the names of people who live in Chicago.

Solution:

SELECT Name FROM people WHERE City = 'Chicago';

Question 4: Find People Older Than 30

Write a query to find people who are older than 30.

Solution:

SELECT Name, Age FROM people WHERE Age > 30;

6. Interview Questions

Here are some interview questions related to SQL queries:

Interview Question 1: What is the purpose of the SELECT statement in SQL?

Answer: The SELECT statement is used to retrieve data from a database table. It allows you to specify which columns of data you want to see.

Interview Question 2: What does the WHERE clause do in SQL?

Answer: The WHERE clause is used to filter records that meet certain conditions. It helps to narrow down the results of a query to only include the data you are interested in.

Interview Question 3: What is the difference between = and LIKE in SQL?

Answer:

  • = is used to find exact matches. For example, WHERE Name = 'John' will only find rows where the name is exactly “John.”
  • LIKE is used for pattern matching. For example, WHERE Name LIKE 'J%' will find all names that start with “J” (like “John”, “Jake”, etc.).

Interview Question 4: How would you get the number of rows in a table?

Answer: You can use the COUNT function to count the number of rows in a table. For example:

SELECT COUNT(*) FROM people;

7. Next Steps

Great job! You’ve just written your first SQL queries. As you continue to learn, you can explore:

  • Joining multiple tables
  • Sorting data with ORDER BY
  • Grouping data with GROUP BY
  • Updating and deleting data

SQL is a powerful language, and learning it will give you the ability to work with databases and manage information efficiently.

Happy querying!