SQL Queries

SQL Queries

Query 1:

USE IMBD

Explanation:

Command Purpose: The USE command sets the active database to IMBD.

Key Points:

This is crucial because SQL operations are database-specific.

Any subsequent queries will target the IMBD database.

When to Use: At the start of a session or after switching to a new database.

---

Query 2:

SHOW TABLES

Explanation:

Command Purpose: This lists all the tables within the currently selected database (IMBD).

Logic:

SQL retrieves metadata about the database to show the table names.

When to Use: When you want to explore the structure or identify available tables for querying.

---

Query 3:

DESCRIBE movies

Explanation:

Command Purpose: This retrieves the schema of the movies table.

Details:

It shows column names, data types, whether the column allows NULL values, and any constraints like PRIMARY KEY.

When to Use: Before querying a table, especially if you need to confirm its structure.

---

Query 4:

SELECT * FROM movies

Explanation:

Command Purpose: The SELECT command retrieves data. Here, * means all columns are selected.

Logic:

Fetches every column and row from the movies table.

This can be inefficient for large datasets.

When to Use: Useful during initial exploration of data.

---

Query 5:

SELECT name, year FROM movies

Explanation:

Command Purpose: Fetches only the name and year columns from the movies table.

Logic:

Limits the result set to specific columns, reducing unnecessary data retrieval.

When to Use: When you need only a subset of columns.

---

Query 6:

SELECT rankscore, name FROM movies

Explanation:

Same as Query 5 but fetches the rankscore and name columns.

---

Query 7:

SELECT * FROM movies LIMIT 20

Explanation:

Clause Explanation:

LIMIT 20 restricts the result to the first 20 rows.

Purpose: View a small, manageable subset of data for analysis.

When to Use: Testing or sampling large datasets.

---

Query 8:

SELECT * FROM movies LIMIT 20 OFFSET 40

Explanation:

Clause Explanation:

OFFSET 40 skips the first 40 rows.

LIMIT 20 fetches the next 20 rows.

Purpose: Implements pagination.

When to Use: Divide results into pages for better readability.

---

Query 9:

SELECT name, year FROM movies ORDER BY year DESC LIMIT 10

Explanation:

Clause Explanation:

ORDER BY year DESC sorts rows by year in descending order.

LIMIT 10 retrieves only the first 10 rows from the sorted result.

Purpose: Retrieves the most recent 10 movies.

When to Use: To analyze recent data trends.

---

Query 10:

SELECT name, year FROM movies ORDER BY year LIMIT 10

Explanation:

Same as Query 9 but sorts year in ascending order.

---

Query 11:

SELECT DISTINCT genre FROM movies_genres

Explanation:

Command Purpose: Fetches unique genre values.

Logic:

DISTINCT ensures no duplicate values in the result.

When to Use: Summarizing or identifying categories in the data.

---

Query 12:

SELECT DISTINCT first, last FROM directors

Explanation:

Retrieves unique combinations of first and last names.

---

Query 13:

SELECT name, year, rankscore FROM movies WHERE rankscore > 9

Explanation:

Clause Explanation:

WHERE filters rows where the rankscore is greater than 9.

Logic:

Only high-ranking movies are included in the result.

When to Use: For performance analysis or identifying top-rated items.

---

Query 14:

SELECT name, year, rankscore FROM movies WHERE rankscore > 9 ORDER BY rankscore DESC LIMIT 20

Explanation:

Same as Query 13 but sorts by rankscore in descending order and limits results to the top 20.

---

Query 15:

SELECT * FROM movies_genres WHERE genre = 'Comedy'

Explanation:

Fetches all rows where genre is exactly 'Comedy'.

---

Query 16:

SELECT * FROM movies_genres WHERE genre = 'Comedy' LIMIT 20

Explanation:

Adds a limit of 20 rows for brevity.

---

Query 17:

SELECT * FROM movies_genres WHERE genre <> 'Horror'

Explanation:

Operator: <> means "not equal to."

Filters out rows where genre is 'Horror'.

---

Query 18:

SELECT name, year, rankscore FROM movies WHERE rankscore IS NULL LIMIT 20

Explanation:

Retrieves rows with missing rankscore values.

---

Query 19:

SELECT name, year, rankscore FROM movies WHERE rankscore IS NOT NULL LIMIT 20

Explanation:

Opposite of Query 18; retrieves rows with valid rankscore.

---

Query 20:

SELECT name, year, rankscore FROM movies WHERE rankscore > 9 AND year > 2020

Explanation:

Operator: AND combines two conditions.

Filters rows where both rankscore > 9 and year > 2020.

---

Query 21:

SELECT name, year, rankscore FROM movies WHERE NOT year <= 2000 LIMIT 20

Explanation:

Filters rows where year is greater than 2000.

---

Query 22:

SELECT name, year, rankscore FROM movies WHERE rankscore > 9 OR year > 2020

Explanation:

Operator: OR retrieves rows where either condition is true.

要查看或添加评论,请登录

Raj Kishore Agrawal的更多文章

  • Entity- Relationship Diagram

    Entity- Relationship Diagram

    Q1) What is ER Diagram Q2) What use E-R Diagram ? Q3) Symbols used in ER Diagram ? Q4) Components of ER Diagram ? Q5)…

    1 条评论
  • Introduction to Cloud Computing and Its Applications

    Introduction to Cloud Computing and Its Applications

    In today’s fast-paced, technology-driven world, cloud computing has emerged as a game-changer, transforming how…

  • The Second Stage of Data Projects: A Deep Dive into ETL

    The Second Stage of Data Projects: A Deep Dive into ETL

    In the data journey, after understanding the use case, the next critical step is ETL, which stands for Extract…

  • The First Stage of Data Projects: Understanding Use Cases

    The First Stage of Data Projects: Understanding Use Cases

    Every impactful data project begins with a critical yet often overlooked step: understanding the use case. This isn’t…

  • Understanding Limit and Offset in Database Queries

    Understanding Limit and Offset in Database Queries

    In the realm of database management, particularly when dealing with SQL (Structured Query Language), the concepts of…

  • Excel Dashboard

    Excel Dashboard

    What is an Excel Dashboard? An Excel Dashboard is a dynamic, interactive tool created within Microsoft Excel to display…

  • Problem Statements: A Comprehensive Guide

    Problem Statements: A Comprehensive Guide

    Introduction In any project, whether in business, technology, or even personal endeavors, a clear problem statement…

  • Understanding the Execution Cycle of an SQL Query: A Key to Optimizing Performance

    Understanding the Execution Cycle of an SQL Query: A Key to Optimizing Performance

    In the fast-paced world of data-driven decision-making, database query performance can make or break your ability to…

  • Introduction to SQL

    Introduction to SQL

    SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in…

  • Database Management System

    Database Management System

    Introduction to Data Data refers to raw facts, figures, or information that can be collected, processed, and analysed…

    1 条评论

社区洞察

其他会员也浏览了