- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to count records that begin with specific letters
Let us first create a table −
mysql> create table DemoTable775 ( FirstName varchar(100) ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable775 values('Adam') ; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable775 values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable775 values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable775 values('Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable775 values('Adan'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable775;
This will produce the following output -
+-----------+ | FirstName | +-----------+ | Adam | | Carol | | John | | Sam | | Adan | +-----------+ 5 rows in set (0.00 sec)
Following is the query to count records that begin with specific letters -
mysql> select count(*),substring(FirstName,1,2) from DemoTable775 group by substring(FirstName,1,2);
This will produce the following output -
+----------+--------------------------+ | count(*) | substring(FirstName,1,2) | +----------+--------------------------+ | 2 | Ad | | 1 | Ca | | 1 | Jo | | 1 | Sa | +----------+--------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to select records that begin with a specific value in MySQL?
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL query to count rows with a specific column?
- MySQL SELECT query to return records with specific month and year
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to alphabetize records and count the duplicates?
- MySQL query to select records beginning from a specific id
- How to restrict MySQL `LIKE` operator to begin with specific characters?
- MongoDB query to retrieve records from a collection named with letters and numbers
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- MySQL query to get the count of distinct records in a column
- MySQL query to fetch specific records matched from an array (comma separated values)
- MySQL query to select records with a particular date?
- MySQL query to display only the records that contains single word?
- MySQL query for grouping and summing the values based on specific records

Advertisements