1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

How to do queries in MySQL

  • ScientificTools.org
  • November 14, 2023 at 5:21 PM
  • 785 times read
Contents [hideshow]
  1. Understanding Queries
  2. Basic Query Structure
    1. Example: Selecting Data
    2. Example: Selecting Specific Columns
  3. Filtering Data with WHERE
    1. Example: Filtering by a Condition
    2. Example: Using Logical Operators
  4. Sorting Data with ORDER BY
    1. Example: Sorting in Ascending Order
    2. Example: Sorting in Descending Order
  5. Aggregating Data
    1. Example: Counting Records
    2. Example: Finding Maximum and Minimum
  6. Joining Tables
    1. Example: Inner Join
  7. Using Subqueries
    1. Example: Subquery

In this guide, we will introduce you to the basics of MySQL queries and provide some practical examples.

Understanding Queries

A query is a request to access or manipulate data stored in a database. In MySQL, you primarily use SQL (Structured Query Language) to write these queries.

Basic Query Structure

The basic structure of a query involves the SELECT statement, which retrieves data from the database.

Example: Selecting Data

To retrieve all data from a Users table:

SQL
SELECT * FROM Users;

In this example, * means "all columns."

Example: Selecting Specific Columns

To retrieve only the Name and Email columns:

SQL
SELECT Name, Email FROM Users;

Filtering Data with WHERE

The WHERE clause filters records that meet certain criteria.

Example: Filtering by a Condition

To find users named 'John Doe':

SQL
SELECT * FROM Users WHERE Name = 'John Doe';

Example: Using Logical Operators

To find users named 'John Doe' or 'Jane Doe':

SQL
SELECT * FROM Users WHERE Name = 'John Doe' OR Name = 'Jane Doe';

To find users aged 30 and above:

SQL
SELECT * FROM Users WHERE Age >= 30;

Sorting Data with ORDER BY

The ORDER BY clause is used to sort the result set.

Example: Sorting in Ascending Order

To sort users by their names in alphabetical order:

SQL
SELECT * FROM Users ORDER BY Name;

Example: Sorting in Descending Order

To sort users by age in descending order:

SQL
SELECT * FROM Users ORDER BY Age DESC;

Aggregating Data

Aggregation functions perform a calculation on a set of values and return a single value.

Example: Counting Records

To count the number of users:

SQL
SELECT COUNT(*) FROM Users;

Example: Finding Maximum and Minimum

To find the oldest user's age:

SQL
SELECT MAX(Age) FROM Users;

To find the youngest user's age:

SQL
SELECT MIN(Age) FROM Users;

Joining Tables

Joins are used to combine rows from two or more tables.

Example: Inner Join

To list users along with their orders from an Orders table:

SQL
SELECT Users.Name, Orders.OrderID
FROM Users
INNER JOIN Orders ON Users.UserID = Orders.UserID;

This query joins Users and Orders based on the UserID.

Using Subqueries

Subqueries are queries nested inside another query.

Example: Subquery

To find users who have placed an order:

SQL
SELECT Name FROM Users
WHERE UserID IN (SELECT UserID FROM Orders);
  • Previous Article How to create, modify, rename and delete tables using MySQL
  • Next Article What is a MySQL subquery and how are subqueries executed in MySQL?

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)

Tags

  • MySQL queries
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™