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)