673,821 questions
-2
votes
0
answers
39
views
Find the number that appears most often, considering 2 columns [duplicate]
Question from LeetCode SQL #602 using MySQL
https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/?envType=problem-list-v2&envId=m8baczxh
My code:
# Write your MySQL query ...
0
votes
3
answers
117
views
Index being ignored when filtering and ordering by different columns?
I have a MySQL table with about 5 million rows:
CREATE TABLE orders (
id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL,
status VARCHAR(20) NOT NULL,
created_at DATETIME NOT NULL,
...
Best practices
1
vote
6
replies
160
views
Update column with default value set
Here is the example of a table:
DROP TABLE IF EXISTS members CASCADE;
CREATE TABLE members (
id SERIAL NOT NULL,
creation_date TIMESTAMPTZ DEFAULT NOW() NOT NULL,
update_date ...
Best practices
1
vote
2
replies
115
views
Best practices for SQL Beginner
What are the best practices that saved you time when you started Data science / Data analysis for the first time?
I want to learn for the first time and don't know how to start and what are the first ...
0
votes
1
answer
135
views
If product code does not exist in first part of query pull a declared value from another table for that product code
The first table I have stores fixed prices on product codes SellPriceRule and I show the query I have to get the list I want. The problem is for certain customers - using 1 as an example 7000192 - the ...
0
votes
1
answer
164
views
Add a column to select query but exclude from grouping
I have the following query:
SELECT
MAX(F.Surname + ', ' + F.First_Name) AS Female,
ISNULL(MAX(M.Surname + ', ' + M.First_Name),'') AS Male,
MAX(CP.Comp_Date) AS Comp_Date,
H....
-9
votes
0
answers
155
views
SQL query to get total usage of items using multiple tables and math [closed]
I am working on project where a user needs to be able to extract a monthly usage of item(s).
The setup for item association is as followed (I'll only mention relevant information):
The user can create ...
Advice
1
vote
4
replies
158
views
How to show only the first instance of a SQL count?
In SQL Server, I have table myTable with fields patientID, orderID, and orderDesc. patients can have multiple orders with individually variable descriptions.
I’m requested to write a query to select ...
Advice
0
votes
6
replies
140
views
Optimizing Month Naming
Can I get any advice on how to optimize the naming for the months in the following code:
(The date convention with the database is 'YYYY-MM-DD')
Select
strftime('%Y-%m', order_date) AS month,
COUNT(...
-11
votes
0
answers
142
views
Movie recommendation database - select top movies [duplicate]
create table Movies
(
movie varchar(100),
genre varchar(100)
)
create table Actors
(
actorName varchar(100),
movie varchar(100)
)
create table Users
(
userName varchar(...
Advice
0
votes
4
replies
129
views
Why does my BigQuery query return NULL for AVG() when the column has values?
Problem details:
I'm working with a weather dataset in BigQuery and trying to calculate the average temperature for a specific date range. The temperature column contains numeric values, but my AVG() ...
Advice
0
votes
3
replies
120
views
Find the time taken based on the start and end times
I have the start and end times, along with other columns on the dataset. I want to know what SQL function to use to find the duration of the intervals. Some rows in the dataset have start and end ...
Best practices
0
votes
6
replies
130
views
Merge rows of a SQL table to fill null with existing values
I have a SQL table like this
Id
A
B
1
value1
null
1
null
value2
Is there a nice way to make a select query returning
Id
A
B
1
value1
value2
Of course, I have many more rows lines and many more columns ...
Advice
1
vote
7
replies
157
views
Use INNER JOIN or Sub-queries in a more efficient way
Products:
ProductID
ProductName
1
Product1
2
Product2
Tags:
TagID
TagName
1
Tag1
2
Tag2
ProductTags (Bridge table between Products and Tags):
ProductID
TagID
1
2
1
2
ProductVariants:
VariantID
...
1
vote
2
answers
191
views
Query `select count(distinct columnname)` returns zero for non-admin user
For a non-admin user, with 'Select' permissions on a 'View', they are able to count the number of rows and display all the contents of a SQL 'View':
select * from dbo.BackupInfoView;
select count(*) ...