EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Commands
Secondary Sidebar
MySQL Tutorial
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL?Table?Size
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

MySQL Commands

By Priya PedamkarPriya Pedamkar

MySQL Commands

Introduction To MySQL Commands

MySQL is an open-source widely used relational database management system that helps to deliver applications with high performance, and scalable web-based and embedded database applications to the customers. It is widely used as a database component of the software stack for a web application. MySQL Commands are very powerful and we will have a look into MySQL commands which are very helpful and consequential for every developer to know and use these queries to interact with the system and MySQL database. It is based on a structured query language (SQL) and it will support and run on Linux, UNIX, and Windows.

Basic MySQL Commands

1. Write a query to create a table country with column names country name, country id, and region id?

Create a table country in MySQL is done by using the below query and the output is as following:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Query:

CREATE TABLE countries(
countryname varchar(60),
countryid varchar(4),
regionid decimal(10,0));

Output:

CREATE TABLE

2. How to get a list of all databases present?

We can get a list of all running databases in MySQL using the below query

Query:

Show databases;

Output:
database present

3. How to get all tables in a database using MySQL?

We can get all the tables present in a database in MySQL using the below query:

Query:

Show tables;

Output:
database using MySQL

4. How to know all the filed names and types of tables in MySQL?

We can get all the filed names and type of a table in MySQL Command using the below query

Query
describe os_users;

Here os_users is a table with filed names u_id and password and the output is as below:

Output:
table in MySQL

5. How to delete a table and a database in MySQL?

We can delete a table using the below MySql Command query

Query to delete a table:

Drop table lookup;

Where lookup is a table name. We can delete a database using the below query

Query to delete a database:

Drop database users;

Where a user is a database

Output for delete tables is:

database in MySQL

6. How to get data from a table where a particular field is filled with data “MyIsam”. We need to get the creation time and engine field for where the engine field is filled with “MyIsam”.

We can write a MySQL Command query for the above requirement as below: Let us say we have a table XYZ with fields creation_time and engine and the engine filed is filled with string data and the creation_time field is filled with date and time.

Query:

Select create_time, engine from xyz where engine=”MyIsam”;

The above query will give creation_time and engine where engine field is filled with “MyIsam”

Output:

MyIsam

7. Query to get all fields from a table where a name is ‘tecmint’ and web address is ‘tecmint.com’?

Let us consider a table as XYZ with field’s id, name, and web address.

We will get the fields of a table where the name matched as “tecmint” and web address is matched as “tecmint.com”

Query:

Select * from xyz where name=”tecmint” and webaddress=”tecmint.com”;

The above query will display all the fields where the field name is matched with tecmint and web address is matched with tecmint.com

Output:

tecmint

8. How to know the total number of rows in a table?

We can the number of rows in a table using the query as below:

Query:

Select count(*) from Tables;

Output:

count()

9. How to select particular filed rows in a table?

We can select a particular filed let us say uid from a table called os_users is

Query:

Select uid from table os_users;

Output:

os_users

10. How to use a particular database in MySQL?

We can switch to use a particular database using the below query

Query:

Use a database cloud;

Output:

database cloud

Intermediate MySQL Commands

There are several other popular MySQL commands that the PHP developer also uses, which are not very basic but work with MySQL more. Some of the following types of intermediate MySQL commands:

11. Query to get a specific field from a table where a constraint is matched( emp_id=100)?

Query:

Select emp_id, emp_name from table employee where emp_id=100;

12. Query to order the results from a table based on certain constraint and using ‘order by’?

Query using with orderby for sorting the results from a table is

Query:

Select emp_id, emp_name from employee where city="Hyderabad" order by emp_id;

13. Query to sort the results from a table based on some constraint and using “Group by”?

Query using “Group By” to sort the results from a table is as below:

Query:

Select name, age from patients where age > 45 group by age order by name;

14. Query to get the total number of customers from a customer’s table?

Query to get all the customers from a customer table is:

Query:

Select count(customer_id), country from customers group by country;

15. Query to calculate the total sum of the salary given to each employee in a company?

Query:

Select sum(salary) from employee where emp_age > 30;

16. Query to list all the views available in the schema?

Query:

Select * from myschema.views;

17. How to create a view from a table?

Query to create a view is as below:

Query:

Create view A_students as select student_name, student_id from student where GPA > 80;

18. How to update a view?

Query to update the existing view is as below:

Query:

Create or replace view product_list As select product_id, Product name, category from products where discount=no;

19. Query to display primary keys from a table?

The following displays all the fields in a table’s primary key:

Query:

Select * from Sys. objects where type=’PK’;

20. Query to display all the user tables in a system?

Query:

Select * from Sys. objects where type=’u’;

Advanced MySQL commands

21. Why IFNULL() statement is used in MySQL?

The IFNULL() statement will check its first argument and return if it is not a null or second argument.

Query:

Select name, IFNULL(id,’unknown’) As ‘id’ from the taxpayer;

22. Query to show only five rows from the result of a query in MySQL?

In order to achieve this we need to use LIMIT in the query as below:

Query:

Select * from students limit 5

23. Query to select all users except one user using the not operator?

Query using not operator is:

Query:

Select * from users where user_name !=SAM;

24. Query to get the present date in MySQL?

The following query will give the current date

Query:

Select current_date();

25. How to export all tables to an XML file in MySQL?

We need to use –e option to export all the tables to an XML file as below query:

Query:

Mysql –u user_name -xml -e ‘select * from table_name’ > tables.xml

Tips and Tricks to use MySQL Commands

Some common users who frequently use MySQL commands normally use some tips and tricks to correctly use MySQL commands output. These types of tricks usually solve some user-specific queries and display the execution output to understand it correctly. Some of the key tricks most commonly used are:

  • Add Explain statement before the select queries as it gives a better explanation about what query is exactly doing.
  • Need to enable query cache as it improves the performance of the execution of queries and it is one of the best methods for database performance.
  • Use stack_trace in MySQL which will help you to differentiate between bugs, track, and fix them.
  • We can take the database backup using a mysqldump command in MySQL which will help you to keep track and backup.

Conclusion

It’s an overview of MySQL commands of different levels and tips and tricks to use them. I hope you will have a good understanding and knowledge about the MySQL commands and how to use them after reading this article.

Recommended Articles

This was a guide to MySQL commands. We discussed basic commands and some MySQL advance commands here. You can also look at the article below for more information:

  1.  Selenium commands
  2. You Should Try Pig Commands
  3. TopMost Sqoop Commands
  4. Tableau Commands
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more